0

I'm trying to "reload" an IndexedContainer with new fresh data, but get a NPE.

I have a addItemsToContainer(List<Person> persons) method that I use to populate my container.

Let's say my Person list contains 100 person objects. When I first call addItemsToContainer(List<Person> persons), my container is populated and everything is fine.

However, the second time I call the method, I get a NullPointerException in setValue().

Now, it seems like removeAllItems() do remove all items, but all ItemId's are left in the container. This means that the second time I call the method, the first automatically generated ItemId is 101. Is this the reason I get the NPE? When debugging, I can see that I get the proper value back from person.getId().

My addItemsToContainer method

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());
    }
}

The stacktrace (ExampleMainTableContainer.java:94 is the statement I marked in the addItemsToContainer method)

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
Roger
  • 2,684
  • 4
  • 36
  • 51
  • In which line exactly does the NPE appear? – Daniel S. Sep 30 '13 at 15:50
  • Oh, sorry. It gets thrown when I'm trying to set the value. Please see my updated original post. NOTE: The NPE gets thrown the *second* time the addItemsToContainer-method is called, not the first. – Roger Sep 30 '13 at 16:05
  • 1
    Please decompose the line where the NPE gets thrown as far as possible by using local variables and doing one thing at a time. That means do IndexedContainer c = getContainerProperty(...); Object personId = person.getId(); c.setValue(personId); all on different lines. then have a look which part causes the NPE. – Daniel S. Sep 30 '13 at 18:55
  • Yes I already did. In my original post I do state that it happens in the `setValue()` method, which is a method of the Vaadin Property Interface. I cannot debug more than this - as soon as I get the value back from person.getId() the NPE is thrown. – Roger Sep 30 '13 at 19:24
  • Please, post full stack trace here. Also, dealing with NPE, you need to specify exactly which of these objects is null. Is it container property or person? – default locale Oct 01 '13 at 07:51
  • I agree with default locale: Please post full stack trace. – Daniel S. Oct 01 '13 at 08:27
  • Please see my updated OP, where I posted the stacktrace. – Roger Oct 01 '13 at 09:55
  • 1
    Sorry for asking this again. Are you sure that `getContainerProperty(itemId, "Id")` doesn't return null? Can you check it? – default locale Oct 01 '13 at 12:48
  • @Daniel S. @default locale You guys were right. getContainerProperty(...) did return null. I dug a little deeper and found this: `if (isFiltered()) { return getFilteredItemIds(); } else { return getAllItemIds();}` ... which means it returns an empty list --> ultimately leading to the getContainerProperty(...) method returning null, because I did actually filter the items in the container before this. Thanks a lot for your input and help, it sure lead me to find the cause of the problem! – Roger Oct 01 '13 at 16:30

1 Answers1

1

The method getContainerProperty(...) returned null, leading to the NPE.

This happened because I filtered my container prior to the second call to addItemsToContainer(...).

When I add a new item to my container, it gets added to allItemIds - the filteredItemIds is left empty. Now, when getContainerProperty(...) is called, it does a check if the container was filtered. If it was filtered, it returns filteredItemIds instead of allItemIds. This meant, of course, that this statement returned an empty list of objects, leading to ´getContainerProperty(...)´ returning null - which finally lead to the NPE.

The solution was to simply add removeAllContainerFilters(); before my ´getContainerProperty(...)´ calls.

Roger
  • 2,684
  • 4
  • 36
  • 51