Search code examples
javagwtguicegwt-gin

GIN injection failure


I am using google-gin in my application and it works fine except from one case and I don't understand why. Basically, I inject a toolbar widget I created into my view and it works fine. I see my toolbar with all the different buttons but when I click on the Home button, I have a NullPointerException which means the eventBus is null and therefore the injection in HomeButton did not work.

My view:

public MyView extends AbstractView{
    @Inject
    EditorToolbar toolbar; 
    // ...
}

My Toolbar widget:

public class EditorToolbar extends HorizontalLayoutContainer {
    private HomeButton homeBtn;
    private ToolbarIconButton saveButton;

    private final static int BUTTON_RIGHT_MARGIN=5;

    public EditorToolbar() {
        homeBtn = new HomeButton();
        saveButton = new ToolbarIconButton("Save",AppImages.INSTANCE.saveBW());

        this.add(homeBtn, new HorizontalLayoutData(-1, -1, new Margins(0, BUTTON_RIGHT_MARGIN, 0, 0)));
        this.add(saveButton, new HorizontalLayoutData(-1, -1, new Margins(0, BUTTON_RIGHT_MARGIN, 0, 0)));
    }

    public HandlerRegistration addSaveHandler(SelectEvent.SelectHandler handler){
        return saveButton.addSelectHandler(handler);
    }
}

The button where the injection of eventBus does not work:

public class HomeButton extends ToolbarIconButton {

    @Inject
    private MetadataEditorEventBus eventBus;

    public HomeButton() {
        super("Back to home page", AppImages.INSTANCE.home());
        setToolTip("Navigate back to the home page");
        setWidth(130);
        bindUI();
    }

    private void bindUI() {
        addSelectHandler(new SelectEvent.SelectHandler() {
            @Override
            public void onSelect(SelectEvent selectEvent) {
                eventBus.fireBackToHomePageEvent();
            }
        });
    }
}

My GIN module:

public class MetadataEditorGinModule extends AbstractGinModule {

    @Override
    protected void configure() {
        bind(com.google.web.bindery.event.shared.EventBus.class).to(MetadataEditorEventBus.class);
        // bind(com.google.gwt.event.shared.EventBus.class).to(MetadataEditorEventBus.class);
        bind(MetadataEditorEventBus.class).in(Singleton.class);

        bind(com.google.web.bindery.requestfactory.shared.RequestFactory.class).to(MetadataEditorRequestFactory.class);

        bind(com.google.gwt.place.shared.PlaceController.class).toProvider(PlaceControllerProvider.class).in(Singleton.class);

        bind(ActivityDisplayer.class).to(MetadataEditorAppDisplayer.class).in(Singleton.class);
        bind(MetadataEditorAppView.class).in(Singleton.class);
    }
}

Does anybody know why I can't get this to work?


Solution

  • You're instantiating the HomeButton widget, this way you're not using GIN and will fail to inject anything.

    You will have to also inject the HomeButton instance on your EditorToolbar. Since the HomeButton is required (not a optional injection), add to the constructor.

    @Inject
    public EditorToolbar(HomeButton homeButton) {
        homeBtn = homeButton;
        //... rest of the code as usual.
    }