JavaScript error: this.getStyleHandle() is null

The following code causes a “this.getStyleHandle() is null” exception - but only when I view the compiled code (I tried both IE 7 and FF 3). Running it in GWT’s hosted browser works fine.

  public void onModuleLoad() {
    IButton btnCreatePkg = new IButton("Click me");
    btnCreatePkg.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        Window wnd = new Window();
        Label lblTitle = new Label();
        wnd.addItem(lblTitle);
        lblTitle.setMargin(5);  // <=== This line causes the error
        wnd.show();
      }
    });
    RootPanel.get().add(btnCreatePkg);
  }

When I call SmartClient via its JavaScript interface directly, I don’t get the error:

<SCRIPT>
isc.IButton.create({
  title:"Click me", 
  click:"btnClick()"
});

function btnClick() {

  wnd = isc.Window.create({});
  lbl=isc.Label.create();
  lbl.setContents("Hello");
  wnd.addItem(lbl);
  lbl.setMargin(5);
  return wnd;
}
</SCRIPT>

I use GWT 1.5.3 on Windows, and the latest nightly build of SmartGWT.

So is this a SmartGWT bug? Or a GWT bug?

It’s a very subtle issue. The order of your calls resulted in the Label widget being created when added to the window, yet the window wasn’t drawn. Try the following. Here the label object is configured with the margin settings before it is added to the window. (setting the the label margin after the window is shown will also work)

  public void onModuleLoad() {
    IButton btnCreatePkg = new IButton("Click me");
    btnCreatePkg.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        Window wnd = new Window();
        Label lblTitle = new Label();
        lblTitle.setMargin(5);
        wnd.addItem(lblTitle);
        wnd.show();
      }
    });
    RootPanel.get().add(btnCreatePkg);
  }

This will be handled in a future SmartGWT build.