I’m designing a simple application in GWT and have different views in the client side. Each view is extended from SimplePanel and will be added to the RootPanel as needed.
My first view is the Login class:
public class Login extends SimplePanel
{
private final TextBox txt_login;
private final PasswordTextBox txt_password;
private final Button btn_login;
private final Grid main_grid;
Login()
{
super();
txt_login=new TextBox();
txt_password=new PasswordTextBox();
btn_login=new Button("Login");
main_grid=new Grid(3,2);
main_grid.setWidget(0, 0, new HTML("Login"));
main_grid.setWidget(1, 0, new HTML("Password"));
main_grid.setWidget(0, 1, txt_login);
main_grid.setWidget(1, 1, txt_password);
main_grid.setWidget(2, 1, btn_login);
setWidget(main_grid);
}
}
And I try to add it to the RootPanel in my entry point class with the following code:
Login login_box=new Login();
RootPanel.get().add(login_box);
But I end up getting the following error in runtime:
java.lang.NullPointerException: null
at com.google.gwt.user.client.ui.AttachDetachException.tryCommand(AttachDetachException.java:72)
at com.google.gwt.user.client.ui.Panel.doAttachChildren(Panel.java:170)
at com.google.gwt.user.client.ui.Widget.onAttach(Widget.java:345)
at com.google.gwt.user.client.ui.Widget.setParent(Widget.java:475)
at com.google.gwt.user.client.ui.Panel.adopt(Panel.java:127)
at com.google.gwt.user.client.ui.ComplexPanel.add(ComplexPanel.java:97)
at com.google.gwt.user.client.ui.AbsolutePanel.add(AbsolutePanel.java:97)
at de.enercon.epi.client.Epi.onModuleLoad(Epi.java:15)
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.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:405)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)
Curiously I’m perfectly able to get it working if I extend the Login class from DialogBox instead of the SimplePanel.
What could be causing it?
I’m not sure what your problem is, but I would suggest extending
Compositeinstead ofSimplePanel, and then the problem should just go away and you’ll be following best practices 🙂 http://googlewebtoolkit.blogspot.com/2009/05/widget-best-practices-widget-building.html