Adharsh
Adharsh

Reputation: 21

How to navigate between views in Vaadin v8?

Can some let me know why I am receiving a NullPointerException when we try to navigate from Login to HomePage.

The Login Page is which has a NavigaeTO method pointing towards HomePage is below

public class MyUI extends UI implements View {

    Navigator navigator;

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout vlayout = new VerticalLayout();

        final TextField name1 = new TextField();
        name1.setCaption("Username");


        final TextField name2 = new TextField();
        name2.setCaption("Password");


        final HorizontalLayout hlaylout1 = new HorizontalLayout();
        Button button1 = new Button("Login",
        new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {

                System.out.print("Here is the View String "+NavigatorUI.HOMEVIEW);

             getNavigator().navigateTo(NavigatorUI.HOMEVIEW);
            }
        });
        Button button2 = new Button("Cancel");

        hlaylout1.addComponents(button1, button2);



        vlayout.addComponents(name1,name2, hlaylout1);

        setContent(vlayout);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

The Home Page is which should open when the Login button is clicked is

public class HomeView extends UI implements View {

    Navigator navigator;

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout vlayout = new VerticalLayout();

        final TextField name1 = new TextField();
        name1.setCaption("HomePage");           


        vlayout.addComponents(name1);

        setContent(vlayout);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

And the Navigator class is

public class NavigatorUI extends UI {

    Navigator navigator;
    public static final String HOMEVIEW = "HOMEVIEW";

    @Override
    protected void init(VaadinRequest request) {
        getPage().setTitle("Navigation Example");

        // Create a navigator to control the views  
        navigator = new Navigator(this, this);

        // Create and register the views
        navigator.addView(HOMEVIEW, new HomeView());
        navigator.addView("", new MyUI());
 //       navigator.addView(MAINVIEW, new MainView());

}

}

I am receiving the NullPointerException for the NavigateTo method. Can someone let me know what the issue is?

Upvotes: 0

Views: 77

Answers (1)

Erik Lumme
Erik Lumme

Reputation: 5342

There are a few issues in your code:

  • You define multiple servlets, one is enough
  • All your views extend UI, only the NavigatorUI should extend UI, the others can build the layouts in the constructor
  • Your servlet is configured to use MyUI, but it should be NavigatorUI for that UI to be used
  • You should override getNavigator() in NavigatorUI to return your newly created navigator, or call setNavigator(navigator).
  • You should remove the Navigator navigator field from your views.

Upvotes: 3

Related Questions