How to access getComponent in a derived class in Vaadin

614 views Asked by At

This is my code. I want to do the following. but im getting exception at getComponent(0) in LoginStep1 class. How should i resolve this. Im new in vaadin. if my approach is wrong your guidance required.

public class Login extends VerticalLayout implements View {
    public static String viewName = "login";

    public void enter(ViewChangeEvent event) {
        removeAllComponents();
        CustomLayout viewScreen = new CustomLayout("screens/screen-login");

        Component step1 = new LoginStep1().getLoginStep1();

        viewScreen.addComponent(step1, "login-steps");
        addComponent(viewScreen);
    }
}
@SuppressWarnings("serial")
public class LoginStep1 extends Login {
    public Component getLoginStep1() {
        CustomLayout stepScreen = new CustomLayout("components/screens/login-step1");
        Button loginBtn = CommonComponents.getButton("Login", "btn btn-green btn-block");

        loginBtn.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                // this line gives me exception.
                CustomLayout currentLayout = (CustomLayout) getComponent(0);
                currentLayout.addComponent(new LoginStep2().getLoginStep2(request, posInfoResponse.getBody()), "login-steps");
            }
        });
    }

    stepScreen.addComponent(loginBtn,"login-btn");
    return stepScreen;
}

it gives following exception

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:160)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:408)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:273)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:79)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1409)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:364)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:158)
    ... 29 more
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in com.herman.login.LoginStep1$1 failed.
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
    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:1003)
    at com.vaadin.ui.Button.fireClick(Button.java:377)
    at com.vaadin.ui.Button$1.click(Button.java:54)
    ... 34 more
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.LinkedList.checkElementIndex(Unknown Source)
    at java.util.LinkedList.get(Unknown Source)
    at com.vaadin.ui.AbstractOrderedLayout.getComponent(AbstractOrderedLayout.java:414)
    at com.herman.login.LoginStep1$1.buttonClick(LoginStep1.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    ... 39 more
1

There are 1 answers

0
Morfic On

Some things are still unclear since you haven't answered all the comments, so I'm unsure which of the 3 implementations (Login, LoginStep1 or LoginStep2) you're using as a view for navigation with the navigator component. However, looking at Login & LoginStep1, I can see a something that you may want to reconsider in the current design: you're creating a new instances of your inherited classes and invoking methods on them, instead of using the current instances...

Since the sources for LoginStep2 are not available, I'll try to cover the other 2 scenarios:

1) Suppose you're navigating to an instance of Login.

  • When the enter method is called, a new instance of LoginStep1 is created and added to the component list inside a CustomLayout.

  • When you click the login button, the getComponent(0) is invoked on the new instance of LoginStep1 which does not inherit from the Login instance used in navigation, hence it contains 0 components leading to your exception

2) Suppose you're navigating to an instance of LoginStep1, which extends Login.

  • When the enter event is triggered and the enter method inherited from the superclass is executed, a new instance of LoginStep1 is created and added to the component list inside a CustomLayout.

  • When you click the login button, the getComponent(0) is invoked on the new instance of LoginStep1 which has nothing to do with the current LoginView1 instance used in navigation, hence it contains 0 components leading to your exception


In conclusion, this is more likely related to basic java inheritance concepts than to Vaadin, and the main question may be Component step1 = new LoginStep1().getLoginStep1(); // <= why create a new instance?

public class Login extends VerticalLayout implements View {
    public static String viewName = "login";

    public void enter(ViewChangeEvent event) {
        removeAllComponents();
        CustomLayout viewScreen = new CustomLayout("screens/screen-login");

        Component step1 = new LoginStep1().getLoginStep1(); // <= why create a new instance?

        viewScreen.addComponent(step1, "login-steps");
        addComponent(viewScreen);
    }
}