gpt4 book ai didi

java - NullPointerException 势不可挡

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:10 25 4
gpt4 key购买 nike

我目前正在使用 vaadin 开发一个小型地址表,只是为了稍微了解一下 vaadin。我遇到了一个问题,我想手动对表中的条目进行排序。问题是,当我尝试执行此操作时,我收到 NullPointerException:

item.getItemProperty(Columns.NAME).getValue()

我在这里尝试了一些事情。我检查了 item 且 item 不为 null,item.getItemProperty(Columns.NAME) 也不为 null。因此,当我尝试创建 if 时,我仍然得到 NullPointerException

if(item.getItemProperty(Columns.NAME).getValue() == null){}

该值为空,我明白这一点,但我想将此值赋予其他项目,但我不知道如何做。

SEVERE: Servlet.service() for servlet [com.example.addresstable.AddresstableUI$Servlet] in context with path [/Addresstable] threw exception [com.vaadin.server.ServiceException: java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.vaadin.data.util.IndexedContainer$IndexedContainerProperty.getValue(IndexedContainer.java:898)
at com.example.addresstable.AddressTable.addAddressToTable(AddressTable.java:114)
at com.example.addresstable.AddresstableUI.init(AddresstableUI.java:26)
at com.vaadin.ui.UI.doInit(UI.java:639)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

最佳答案

IndexedContainerItemIndexedContainerItem
当调用它的 getItemProperty 时,结果永远不会为 null:

@Override
public Property getItemProperty(Object id) {
return new IndexedContainerProperty(itemId, id);
}

但是当你调用PropertygetValue时,如果没有itemId或propertyId,它可能为null:

@Override
public T getValue() {
return (T) items.get(itemId).get(propertyId);
}

可能是 Vaadin 错误:link

编辑

如果您想检查容器中是否包含该项目,您可以执行以下操作:

public class MyIndexedContainer extends IndexedContainer {
@Override
public Item getUnfilteredItem(Object itemId) { // make it public
return super.getUnfilteredItem(itemId);
}
}

然后

Table table = new Table();
MyIndexedContainer mic = new MyIndexedContainer();
table.setContainerDataSource(mic);
//...
// check that the container contains the item
if (mic.getUnfilteredItem(item) != null) {
//...
}

关于java - NullPointerException 势不可挡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26216512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com