尝试使用新数据重新加载IndexedContainer时出现NullPointerException

时间:2013-09-30 15:45:21

标签: vaadin vaadin7

我正在尝试使用新的新数据“重新加载”一个IndexedContainer,但获得一个NPE。

我使用addItemsToContainer(List<Person> persons)方法来填充容器。

假设我的人员列表包含100个人对象。当我第一次致电addItemsToContainer(List<Person> persons)时,我的容器已填充,一切正常。

然而,我调用该方法的第二次时间,我在setValue()中得到NullPointerException

现在,似乎removeAllItems() 执行删除所有项目,但所有 ItemId的都保留在容器中。这意味着第二次调用方法时,第一个自动生成的ItemId为101。这是我获得NPE的原因吗?在调试时,我可以看到我从person.getId()获得了正确的值。

我的addItemsToContainer方法

public void addItemsToContainer(List<Person> persons) {

    removeAllItems();

    for (Person person : persons) {
        Object itemId = addItem();
        getContainerProperty(itemId, "Id").setValue(person.getId()); // <-- NPE here
        getContainerProperty(itemId, "Name").setValue(person.getName());
    }
}

堆栈跟踪 ExampleMainTableContainer.java:94是我在addItemsToContainer方法中标记的语句)

com.vaadin.server.ServerRpcManager$RpcInvocationException: Unable to invoke method click in com.vaadin.shared.ui.button.ButtonServerRpc
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:170)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleBurst(ServerRpcHandler.java:214)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:111)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:91)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
    ... 24 more
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in com.example.myapp.web.view.ExampleView$1 failed.
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:167)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:969)
    at com.vaadin.ui.Button.fireClick(Button.java:368)
    at com.vaadin.ui.Button$1.click(Button.java:57)
    ... 29 more
Caused by: java.lang.NullPointerException
    at com.example.myapp.web.view.ExampleMainTableContainer.addItemsToContainer(ExampleMainTableContainer.java:94)
    at com.example.myapp.web.view.ExampleMainTable.addMainTableItems(ExampleMainTable.java:172)
    at com.example.myapp.web.view.ExampleMainTable.experiment(ExampleMainTable.java:86)
    at com.example.myapp.web.view.ExampleView$1.buttonClick(ExampleView.java:208)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    ... 33 more

1 个答案:

答案 0 :(得分:1)

方法getContainerProperty(...)返回null,导致NPE。

这是因为我在第二次调用addItemsToContainer(...)之前过滤了我的容器。

当我向容器中添加新项目时,会将其添加到allItemIds - filteredItemIds为空。现在,当调用getContainerProperty(...)时,它会检查容器是否已过滤。如果已过滤,则会返回filteredItemIds而不是allItemIds。当然,这意味着该语句返回一个空的对象列表,导致'getContainerProperty(...)'返回null - 最终导致NPE。

解决方案只是在我的'getContainerProperty(...)'调用之前添加removeAllContainerFilters();