GWT extending DecoratorPanel and adding to rootPanel
I am new to using gwt, although I am starting to get the hang of things. One perplexing situation I came across today was the following. (This is abbreviated to simplify my question)
public class MyPanel extends DecoratorPanel{
public MyPanel(){
final TextBox tb = new TextBox();
tb .setText("test");
this.add(tb);
tb .setFocus(true);
}
}
public class ClassA implements EntryPoint(){
public void onModuleLoad(){
MyPanel mp = new MyPanel();
RootPanel.get("reference").add(mp);
}
}
for some reason my problem occurs at the line:
RootPanel.get("reference").add(log);
I get a null pointer exception...the lines of the stack above the line that points at the line above is:
java.lang.NullPointerException: null
at com.google.gwt.user.client.ui.Panel.doAttachChildren(Panel.java:163)
at com.google.gwt.user.client.ui.Widget.onAttach(Widget.java:259)
at com.google.gwt.user.client.ui.Widget.setParent(Widget.java:393)
at com.google.gwt.user.client.ui.Panel.adopt(Panel.java:119)
at com.google.gwt.user.client.ui.ComplexPanel.add(ComplexPanel.java:86)
at com.google.gwt.user.client.ui.AbsolutePanel.add(AbsolutePanel.java:80)
So I thought this would just be a simple problem however, when I put all the code from MyPanel into ClassA in the onModuleLoad meth开发者_开发百科od and just created a DecoratorPanel in there, there was no null pointer exception. Why is this, and how can I fix it. It seems like a simple problem but I am not sure which direction to go. Anyone have an idea?
The null pointer exception happens on this line in Panel:
for (Iterator it = iterator(); it.hasNext();) {
The above code works for me without any problem.Can you post the version of gwt and OS you are using.Make sure that you are having following DIV in your html.
<div id="reference"></div>
If everything is fine still it is not working means try the following
RootPanel.get().add(mp);
If above all fails log a bug in http://groups.google.com/group/Google-Web-Toolkit?pli=1 that website
So, it was due to a an incorrect jar in the library I believe. Either way it works now. I have one other question though. How can you add a panel to the root panel from a class that doesnt have onModuleLoad. Is it possible?
As BlackPanther said, the most likely error is that you are missing <div id="reference"></div>
or some other HTML element with that id in your base html page.
I've tried
/**
* This is the entry point method.
*/
public void onModuleLoad() {
// MyPanel mp = new MyPanel();
DecoratorPanel mp = new DecoratorPanel();
final TextBox tb = new TextBox();
tb.setText("test");
mp.add(tb);
tb.setFocus(true);
RootPanel.get("reference").add(mp);
}
and it will throw the same error.
RootPanel.get("reference")
is returning null as the element is not found in the HTML page.
精彩评论