NullPointerException in guice3 when using inheritance, listeners, and static injection

188 views
Skip to first unread message

Michael Burton

unread,
Oct 21, 2011, 1:08:40 PM10/21/11
to google...@googlegroups.com, Jon Perlow
A user of RoboGuice ran into the following error awhile back.  As best we can tell, it appears this might be a bug in Guice3?  Can anyone explain what might be the issue if we're doing anything wrong?

Here's the code:

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;

public class GuiceTest {

  public static void main(String[] args) {
    TestApp testApp = new TestApp();
    Guice.createInjector(new MyModule1(testApp), new MyModule2(testApp));
  }

  private static class BaseApp {
  }


  // Extends BaseApp
  private static class TestApp extends BaseApp {
  }

  

  private static class MyModule1 extends AbstractModule {

    private final BaseApp app;

    public MyModule1(BaseApp app) {
      this.app = app;
    }

    @Override
    protected void configure() {
      bind(BaseApp.class).toInstance(app);
      bindListener(Matchers.any(), new MyListener());
      requestStaticInjection(SomeOtherClass.class);
    }
  }

  private static class MyModule2 extends AbstractModule {

    private final TestApp app;

    public MyModule2(TestApp app) {
      this.app = app;
    }

    // Does nothing other than binding app to TestApp, which is a binding that's never used
    @Override
    protected void configure() {
      bind(TestApp.class).toInstance(app);
    }
  }

  // Just an empty listener
  private static class MyListener implements TypeListener {
    public <I> void hear(TypeLiteral<I> iTypeLiteral, TypeEncounter<I> iTypeEncounter) {
    }
  }

  private static class SomeOtherClass {

    @Inject static BaseApp app;

  }
}




When you compile and run, you'll get the following error:

$ javac -cp /Users/mike/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar:/Users/mike/.m2/repository/com/google/inject/guice/3.0/guice-3.0-no_aop.jar:/Users/mike/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar GuiceTest.java
$ java -cp /Users/mike/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar:/Users/mike/.m2/repository/com/google/inject/guice/3.0/guice-3.0-no_aop.jar:/Users/mike/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:. GuiceTest

Exception in thread "main" com.google.inject.CreationException: Guice creation errors:

1) Error in custom provider, java.lang.NullPointerException
  at GuiceTest$MyModule1.configure(GuiceTest.java:37)
  while locating GuiceTest$BaseApp
    for field at GuiceTest$SomeOtherClass.app(Unknown Source)

1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:175)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at com.google.inject.Guice.createInjector(Guice.java:62)
at GuiceTest.main(GuiceTest.java:14)
Caused by: java.lang.NullPointerException
at com.google.inject.internal.Initializer$InjectableReference.get(Initializer.java:147)
at com.google.inject.internal.ConstantFactory.get(ConstantFactory.java:35)
at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1031)
at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
at com.google.inject.Scopes$1$1.get(Scopes.java:65)
at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
at com.google.inject.internal.SingleFieldInjector.inject(SingleFieldInjector.java:53)
at com.google.inject.internal.InjectionRequestProcessor$StaticInjection$1.call(InjectionRequestProcessor.java:116)
at com.google.inject.internal.InjectionRequestProcessor$StaticInjection$1.call(InjectionRequestProcessor.java:110)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024)
at com.google.inject.internal.InjectionRequestProcessor$StaticInjection.injectMembers(InjectionRequestProcessor.java:110)
at com.google.inject.internal.InjectionRequestProcessor.injectMembers(InjectionRequestProcessor.java:78)
at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:170)
... 5 more


Cheers,
Mike

Sam Berlin

unread,
Oct 21, 2011, 1:33:33 PM10/21/11
to google...@googlegroups.com, Jon Perlow
This looks like the bug fixed by http://code.google.com/p/google-guice/source/detail?r=6b7e7187bd074d3f2df9b04e17fa01e7592f295c (issue http://code.google.com/p/google-guice/issues/detail?id=627).

It seems to be a super old bug that's been present at least since Guice 2 and possibly before.  The test added in the fix should explain what causes it.

sam

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/CxXJN4nQwCsJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Sam Berlin

unread,
Oct 21, 2011, 1:34:46 PM10/21/11
to google...@googlegroups.com, Jon Perlow
(But, if you still experience the bug with the latest code from Git, please reopen the bug or create a new one!)
 sam

Michael Burton

unread,
Oct 21, 2011, 5:18:00 PM10/21/11
to google...@googlegroups.com, Jon Perlow
Snapshot fixes it, thanks Sam!

Sam Berlin

unread,
Oct 21, 2011, 5:23:25 PM10/21/11
to google...@googlegroups.com, Jon Perlow
Great, thanks for the confirmation!
 sam
Reply all
Reply to author
Forward
0 new messages