开发者

google-gin a provider needs a dependency. NullPointerException BindingsProcessor.java:498

开发者 https://www.devze.com 2023-04-07 15:50 出处:网络
In my GWT application i\'m trying to setup a DI mechanism wihich would allow me to have all the commonly necessary stuff at hand everywhere. I\'m using google-gin which is an adaptation of guice for G

In my GWT application i'm trying to setup a DI mechanism wihich would allow me to have all the commonly necessary stuff at hand everywhere. I'm using google-gin which is an adaptation of guice for GWT. I have an injector interface defined as this:

@GinModules(InjectionClientModule.class)
public interface MyInjector extends Ginjector {
    public PlaceController getPlaceController();
    public Header getHeader();
    public Footer getFooter();
    public ContentPanel getContent();
    public EventBus getEventBus();
    public PlaceHistoryHandler getPlaceHistoryHandler();
}

My injection modul开发者_Go百科e is this:

public class InjectionClientModule extends AbstractGinModule {
    public InjectionClientModule() {
        super();
    }

    protected void configure() {
        bind(Header.class).in(Singleton.class);
        bind(Footer.class).in(Singleton.class);
        bind(ContentPanel.class).in(Singleton.class);
        bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
        bind(PlaceController.class).toProvider(PlaceControllerProvider.class).asEagerSingleton();
        bind(PlaceHistoryHandler.class).toProvider(PlaceHistoryHandlerProvider.class).asEagerSingleton();
    }
}

When calling MyInjector injector = GWT.create(MyInjector.class); i'm gettign the following exception:

java.lang.NullPointerException: null
at com.google.gwt.inject.rebind.BindingsProcessor.createImplicitBinding(BindingsProcessor.java:498)
at com.google.gwt.inject.rebind.BindingsProcessor.createImplicitBindingForUnresolved(BindingsProcessor.java:290)
at com.google.gwt.inject.rebind.BindingsProcessor.createImplicitBindingsForUnresolved(BindingsProcessor.java:278)
at com.google.gwt.inject.rebind.BindingsProcessor.process(BindingsProcessor.java:240)
at com.google.gwt.inject.rebind.GinjectorGeneratorImpl.generate(GinjectorGeneratorImpl.java:76)
at com.google.gwt.inject.rebind.GinjectorGenerator.generate(GinjectorGenerator.java:47)
at com.google.gwt.core.ext.GeneratorExtWrapper.generate(GeneratorExtWrapper.java:48)
at com.google.gwt.core.ext.GeneratorExtWrapper.generateIncrementally(GeneratorExtWrapper.java:60)
at com.google.gwt.dev.javac.StandardGeneratorContext.runGeneratorIncrementally(StandardGeneratorContext.java:647)
at com.google.gwt.dev.cfg.RuleGenerateWith.realize(RuleGenerateWith.java:41)
at com.google.gwt.dev.shell.StandardRebindOracle$Rebinder.rebind(StandardRebindOracle.java:78)
at com.google.gwt.dev.shell.StandardRebindOracle.rebind(StandardRebindOracle.java:268)
at com.google.gwt.dev.shell.ShellModuleSpaceHost.rebind(ShellModuleSpaceHost.java:141)
at com.google.gwt.dev.shell.ModuleSpace.rebind(ModuleSpace.java:585)
at com.google.gwt.dev.shell.ModuleSpace.rebindAndCreate(ModuleSpace.java:455)
at com.google.gwt.dev.shell.GWTBridgeImpl.create(GWTBridgeImpl.java:49)
at com.google.gwt.core.client.GWT.create(GWT.java:97)

The problem is that the PlaceController class actually depends on one of the other dependencies. I've implemented it's provider like this:

public class PlaceControllerProvider implements Provider<PlaceController> { 
    private final PlaceController placeController;

    @Inject
    public PlaceControllerProvider(EventBus eventBus) {
        this.placeController = new PlaceController(eventBus);
    }

    @Override
    public PlaceController get() {
        return placeController;
    }
}

what should i change for this to work?


Old question but having the same problem I kept falling here. I finally found the way to know which class is messing during ginjection.

When I launch my app in development mode and put stack to Trace, I noticed there is a step called : "Validating newly compiled units". Under this, I had an error but I didn't notice it since I had to expand 2 nodes which weren't even in red color.

The error was "No source code available for type com.xxx.xxxx ...", which was due to a bad import on client side which couldn't be converted to Javascript.

Hope this may help other here !


While I'm not actually seeing how the errors you're getting are related to the PlaceController being injected, I do see that the provider is returning a singleton PlaceController even if the provider were not bound as an eager singleton or in a different scope. The correct way to write that provider would be:

public class PlaceControllerProvider implements Provider<PlaceController> { 
    private final EventBus eventBus;

    @Inject
    public PlaceControllerProvider(EventBus eventBus) {
        this.eventBus = eventBus;
    }

    @Override
    public PlaceController get() {
        return new PlaceController(eventBus);
    }
}

Let guice handle the scoping i.e. "Letting guice work for you".

Other than that, I almost bet that your problem is due to the use of asEagerSingleton. I recommend you try this with just in(Singleton.class) and I further posit that you didn't really need the singleton to be eager. It seems others had problems with the behavior too, there's some indication that it has to do with overusing asEagerSingleton or misunderstanding the @Singleton annotation in a few cases.


I also got a lot of NullPointerException warnings using GIN 1.x with no real explanation of what happened. When I upgraded to gin 2.0 I was told with high accuracy what the error was. You might be helped by upgrading to the 2.0 version that was released a year after you asked this question.


Had the same problem problem, same trace, and the error was that I used "server" classes in my "client" classes, so GIN can't find these classes.

I mean by "server" and "client" the packages in my project.

Hope this could help

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号