1

I'm getting this error when running GWT application:

java.lang.AssertionError: This UIObject's element is not set; you may be missing a call to either Composite.initWidget() or UIObject.setElement()

public class MainView extends Composite implements HeaderPresenter.MyView {
  // Code omitted
}

In the Gin ClientModule.java configure() function I have this code:

bindPresenter(HeaderPresenter.class, HeaderPresenter.MyView.class,
                MainView.class, HeaderPresenter.MyProxy.class);

In the view class the initWidget() is properly called and passed with a widget, what could be causing the error?

quarks
  • 33,478
  • 73
  • 290
  • 513
  • My application used to work with the View that I use in the bindPresenter which is based on UiBinder view. However for some reason I need to use a View that is plain GWT widget which extends from Composite. Now it wont work. – quarks May 03 '12 at 10:48
  • You should post more code about your `MainView` class. – Sydney May 03 '12 at 21:19

2 Answers2

3

This error occurs when UIObject.setElement isn't called. If you are calling Composite.initWidget with a non-null widget, make sure that that widget is setting its own element correctly. If this is a standard GWT widget, it should be doing that, but otherwise it is possible that the widget passed to initWidget isn't set up correctly.

Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
  • I had to code the widget that is inserted in the initWidget() outside of the constructor, i.e. HorizontalPanel panel = new HorizontalPanel() as opposed to initializing it on the construction (although initializing before passing to initWidget ofcourse) – quarks May 04 '12 at 10:39
  • And that is the problem - you are adding the root widget after the composite has been added to its parent, so nothing can be drawn. This is how the system works, initWidget must be called by then. One possible hack could be to initWidget with a panel, then later add the HorizontalPanel to _that_ panel. – Colin Alworth May 04 '12 at 13:32
3

This is how I create a Composite that I will use later in a View.

public class MyCustomBox extends Composite {

    private static MyCustomBoxUiBinder uiBinder = GWT.create(MyCustomBoxUiBinder.class);

    interface MyCustomBoxUiBinder extends UiBinder<Widget, MyCustomBox> {
    }

    public MyCustomBox() {
        initWidget(uiBinder.createAndBindUi(this));
    }
}
Sydney
  • 11,964
  • 19
  • 90
  • 142