我目前正在使用vaadin处理一个小的addresstable,只是为了了解一下vaadin。我遇到了一个问题,我想手动对表格的条目进行排序。问题是,当我尝试这样做时,我得到一个NullPointerException:
item.getItemProperty(Columns.NAME).getValue()
我在这里试了几件事。我检查了项目和项目是否为空,item.getItemProperty(Columns.NAME)为null。所以,当我尝试制作一个如果我仍然得到一个NullPointerException
if(item.getItemProperty(Columns.NAME).getValue() == null){}
该值为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)
答案 0 :(得分:1)
IndexedContainer
' Item
是IndexedContainerItem
当调用它getItemProperty
时,结果永远不会为空:
@Override
public Property getItemProperty(Object id) {
return new IndexedContainerProperty(itemId, id);
}
但是当你致电Property
' getValue
时,如果没有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) {
//...
}