Grid: error message?


Why I am getting this type of error message in new Grid Component?

java.lang.NullPointerException at com.xyz.example.Template.lambda$1(Example.java:370) at com.xyz.Template.Example$$Lambda$3/1213887338.select(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508) at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198) at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161) at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:995) at com.vaadin.ui.Grid.fireSelectionEvent(Grid.java:4883) at com.vaadin.ui.Grid$AbstractSelectionModel.fireSelectionEvent(Grid.java:948) at com.vaadin.ui.Grid$SingleSelectionModel.deselectInternal(Grid.java:990) at com.vaadin.ui.Grid$SingleSelectionModel.deselect(Grid.java:983) at com.vaadin.ui.Grid$SingleSelectionModel.select(Grid.java:960) at com.vaadin.ui.Grid$3.select(Grid.java:3683) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:158) at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118) at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:313) at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:202) at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:95) at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41) at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408) at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:350) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:769) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1125) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1059) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handle(Server.java:485) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:290) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248) at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:606) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:535) at java.lang.Thread.run(Thread.java:745)

What does com.xyz.example.Template.lambda do at Example.java:370?


It does this:

[code]
grid.addSelectionListener(e → {

// Get the item of the selected row

BeanItem item = container.getItem(grid.getSelectedRow());

// Use the item somehow
Notification.show("Selected " +
item.getBean().getName());
});
[/code]On line 370 it does[b]

[/b]exactly this
: item.getBean().getName());

If it produces a
NullPointerException
then either
item
or
item.getBean()
is null. Check what
item
is, check what
grid.getSelectedRow
returns.

I guess it should work in the normal case where you select a row, but I can already point out that it will most likely produce a
NullPointerException
when you deselect a row. Deselection will cause and selection event to fire →
grid.getSelectedRow()
will return null →
container.getItem(null)
will return null →
null.getBean()
will throw a
NullPointerException
.

Thanks for your answer.

I find it weird that item.getBean() is not null and it throws NullPointerException especially when I addSelectionListener for multiple rows.

Also the selectionListener shows only the previous row and not the both rows. Do I have to enable
multi-selection mode?

So basically when I do something like this throws NullPointerException:

grid.addSelectionListener(e -> {    
// Get the item of the selected row     
BeanItem<Person> item = container.getItem(grid.getSelectedRow());     
// Use the item somehow    
Notification.show("Selected " +
item.getBean().getName());
});

grid.addSelectionListener(e -> {    
// Get the item of the selected row     
BeanItem<Person> item = container.getItem(grid.getSelectedRow());     
// Use the item somehow    
Notification.show("Selected " +
item.getBean().getAddress());
});