NullPointerException in Grid using navigateTo inside SelectionListener

I try to navigate to a different view and get a NullPointerException inside my Grid.
To reproduce the error I reduce my class to a minimum

import com.vaadin.event.SelectionEvent;
import com.vaadin.event.SelectionEvent.SelectionListener;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Grid;
import com.vaadin.ui.VerticalLayout;

public class GridTest extends VerticalLayout implements View {

    @Override
    public void enter(ViewChangeEvent event) {
        // Create a grid
        Grid grid = new Grid();

        // Define some columns
        grid.addColumn("name", String.class);
        grid.addColumn("born", Integer.class);

        // Add some data rows
        grid.addRow("Nicolaus Copernicus", 1543);
        grid.addRow("Galileo Galilei", 1564);
        grid.addRow("Johannes Kepler", 1571);

        addComponent(grid);
        
        grid.addSelectionListener(new SelectionListener() {
            
            @Override
            public void select(SelectionEvent event) {
                getUI().getNavigator().navigateTo("");
                
            }
        });
    }

}

By clicking a row I get the following exception

Mai 04, 2015 4:05:20 PM com.vaadin.server.DefaultErrorHandler doDefault Schwerwiegend: java.lang.NullPointerException at com.vaadin.ui.Grid$3.select(Grid.java:3031) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168) at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118) at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:291) at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:184) at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:92) 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: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:472) 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:936) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 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.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744) I get the same exception when using a ItemClickListener instead of a SelectionListener.
Is this a bug or is it not allowed to use navigateTo at this point?

Oliver

Fascinating. The navigateTo() call immediately detaches the view (and the Grid it contains) from the UI, but processing the selection event relies on the Grid being attached after calling the listeners. More exactly, the RPC handler gets a null from getUI() and fails on this line:

JsonObject diffState = getUI().getConnectorTracker()
                        .getDiffState(Grid.this);

The problem probably occurs with any SelectionListener that detaches the Grid from the UI, so it’s not Navigator specific.

That’s a bug! Could you please
file a bug report in Vaadin Trac
? I didn’t notice an existing one about this issue, at least regarding Grid, but it really looks like Grid-specific.

For a workaround, perhaps using a ButtonRenderer (that is, a rendered column with “Show” buttons) would avoid the issue.

I have created a ticket
https://dev.vaadin.com/ticket/17722#ticket
.

With the workaround using a ButtonRenderer it works - but in my real project, my container is a LazyQueryContainer and I can’t add a column.

grid.addColumn("link")

throws

java.lang.IllegalStateException: Container for this Grid is not a default container from Grid() constructor Did you have an idea for this problem?

Hi Oliver,

Grid has basically two modes of operation regarding columns. If a custom data source is not set, Grid uses its own internal container. In this case, the addColumn method adds a column to the Grid and a corresponding property to the internal data source.

On the other hand, if you explicitly set a data source then Grid forbids adding columns that don’t have a matching property in the container. It automatically creates columns based on the container properties, though, so you shouldn’t need to add them manually.