0

我试图用新的数据“重新加载”一个 IndexedContainer,但得到一个 NPE。

我有一种addItemsToContainer(List<Person> persons)用于填充容器的方法。

假设我的 Person 列表包含 100 个人员对象。当我第一次打电话addItemsToContainer(List<Person> persons)时,我的容器已经装满了,一切都很好。

但是,第二次调用该方法时,我NullPointerException在 setValue() 中得到了一个。

现在,似乎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());
    }
}

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

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
4

1 回答 1

1

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

发生这种情况是因为我在第二次调用addItemsToContainer(...).

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

解决方案removeAllContainerFilters();是在我的“getContainerProperty(...)”调用之前简单地添加。

于 2013-10-01T16:47:35.677 回答