views:

385

answers:

3

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 method 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();) {

+1  A: 

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

BlackPanther
As I mentioned this code workes when I put all of the MyPanel code into onModuleLoad in ClassA instead of defining it in a seperate class. However it really isn't an exceptable solution for me to put all my ui in one class you know? hah. I also should have mentioned that the correct ui displays even though the error is present. Any more ideas? I am on vista unfortunately when the error is happening im not sure that should affect anything though because the only difference is where i am defining the ui. What I am trying should be simple..right?
John
I'm tired and just woke up excuse the typos above. i.e. acceptable
John
@John, I've just tried what you said, see my answer, and I'm almost sure that it is related to the html page you are providing in the app. Make sure the browser cache is not playing with you and is using an outdated version of the HTML page. Clear the browser cache to be sure.
Iker Jimenez
A: 

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.

Iker Jimenez
A: 

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?

John
Yes.It is possible.onModuleLoad method is just entry point of your application.RootPanel.get().add(panel) can be anywhere.If you say RootPanel.get("somediv").add(panel) like that it will search for div with id "somediv" .Thats it.There is no link between adding panel to RootPanel and onModuleLoad method.
BlackPanther