使用Gin构建GWT时出现NullPointerException

时间:2012-02-23 21:13:04

标签: java gwt dependency-injection nullpointerexception gin

我的Gin模块包含以下内容:

  @Override
  protected void configure() {
    bind(HistoryWrapper.class).to(HistoryWrapperImpl.class);
    ...
  }

有问题的类型是:

public interface HistoryWrapper {

  void newItem(String token);

}

public class HistoryWrapperImpl implements HistoryWrapper {

  @Override
  public void newItem(String token) {}

}

当我尝试GWT编译时,我收到以下错误:

Compiling module com.foo.FooGame
   Validating newly compiled units
      Ignored 9 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
   Scanning for additional dependencies: file: FooGameHtml.java
      Computing all possible rebind results for 'com.foo.html.client.gin.BarGinjector'
         Rebinding com.foo.html.client.gin.BarGinjector
            Invoking generator com.google.gwt.inject.rebind.GinjectorGenerator
               [ERROR] Generator 'com.google.gwt.inject.rebind.GinjectorGenerator' threw an exception while rebinding 'com.foo.html.client.gin.BarGinjector'
java.lang.NullPointerException
    at com.google.gwt.inject.rebind.BindingsProcessor.createImplicitBinding(BindingsProcessor.java:498)
    at com.google.gwt.inject.rebind.BindingsProcessor.access$900(BindingsProcessor.java:99)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceBindingVisitor.addImplicitBinding(BindingsProcessor.java:982)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceBindingVisitor.visit(BindingsProcessor.java:972)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceBindingVisitor.visit(BindingsProcessor.java:898)
    at com.google.inject.internal.UntargettedBindingImpl.acceptTargetVisitor(UntargettedBindingImpl.java:40)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceElementVisitor.visit(BindingsProcessor.java:827)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceElementVisitor.visit(BindingsProcessor.java:820)
    at com.google.inject.internal.BindingImpl.acceptVisitor(BindingImpl.java:92)
    at com.google.gwt.inject.rebind.BindingsProcessor.createBindingsForModules(BindingsProcessor.java:409)
    at com.google.gwt.inject.rebind.BindingsProcessor.process(BindingsProcessor.java:238)
    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.StandardRebindOracle.rebind(StandardRebindOracle.java:257)
    at com.google.gwt.dev.DistillerRebindPermutationOracle.getAllPossibleRebindAnswers(DistillerRebindPermutationOracle.java:91)
    at com.google.gwt.dev.jdt.WebModeCompilerFrontEnd.doFindAdditionalTypesUsingRebinds(WebModeCompilerFrontEnd.java:96)
    at com.google.gwt.dev.jdt.AbstractCompiler$Sandbox$CompilerImpl.process(AbstractCompiler.java:254)
    at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:444)
    at com.google.gwt.dev.jdt.AbstractCompiler$Sandbox$CompilerImpl.compile(AbstractCompiler.java:173)
    at com.google.gwt.dev.jdt.AbstractCompiler$Sandbox$CompilerImpl.compile(AbstractCompiler.java:288)
    at com.google.gwt.dev.jdt.AbstractCompiler$Sandbox$CompilerImpl.access$400(AbstractCompiler.java:139)
    at com.google.gwt.dev.jdt.AbstractCompiler.compile(AbstractCompiler.java:588)
    at com.google.gwt.dev.jdt.BasicWebModeCompiler.getCompilationUnitDeclarations(BasicWebModeCompiler.java:97)
    at com.google.gwt.dev.jdt.WebModeCompilerFrontEnd.getCompilationUnitDeclarations(WebModeCompilerFrontEnd.java:52)
    at com.google.gwt.dev.jjs.JavaToJavaScriptCompiler.precompile(JavaToJavaScriptCompiler.java:569)
    at com.google.gwt.dev.jjs.JavaScriptCompiler.precompile(JavaScriptCompiler.java:33)
    at com.google.gwt.dev.Precompile.precompile(Precompile.java:284)
    at com.google.gwt.dev.Precompile.precompile(Precompile.java:233)
    at com.google.gwt.dev.Precompile.precompile(Precompile.java:145)
    at com.google.gwt.dev.Compiler.run(Compiler.java:232)
    at com.google.gwt.dev.Compiler.run(Compiler.java:198)
    at com.google.gwt.dev.Compiler$1.run(Compiler.java:170)
    at com.google.gwt.dev.CompileTaskRunner.doRun(CompileTaskRunner.java:88)
    at com.google.gwt.dev.CompileTaskRunner.runWithAppropriateLogger(CompileTaskRunner.java:82)
    at com.google.gwt.dev.Compiler.main(Compiler.java:177)
   [ERROR] Errors in 'src/com/foo/html/client/FooGameHtml.java'
      [ERROR] Line 34:  Failed to resolve 'com.foo.html.client.gin.BarGinjector' via deferred binding
   Scanning for additional dependencies: jar:file:/C:/path/to/eclipse-java-indigo-win32-x86_64%20(1)/eclipse/plugins/com.google.gwt.eclipse.sdkbundle_2.4.0.v201201120043-rel-r37/gwt-2.4.0/gwt-user.jar!/com/google/gwt/core/client/impl/AsyncFragmentLoader.java
      [WARN] For the following type(s), generated source was never committed (did you forget to call commit()?)
         [WARN] com.foo.html.client.gin.BarGinjectorImpl

我在这里做错了什么?评论bind(HistoryWrapper.class)行会使问题消失,但显然我需要对该类型进行绑定。

当我尝试使用@Provides方法时,我遇到了类似的错误。

更新:现在我得到了一个不同的NPE,但我不确定原因:

java.lang.NullPointerException
    at com.google.gwt.inject.rebind.util.KeyUtil.javaToGwtMethod(KeyUtil.java:222)
    at com.google.gwt.inject.rebind.binding.ProviderMethodBinding.setProviderMethod(ProviderMethodBinding.java:60)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceBindingVisitor.visit(BindingsProcessor.java:926)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceBindingVisitor.visit(BindingsProcessor.java:898)
    at com.google.inject.internal.ProviderInstanceBindingImpl.acceptTargetVisitor(ProviderInstanceBindingImpl.java:52)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceElementVisitor.visit(BindingsProcessor.java:827)
    at com.google.gwt.inject.rebind.BindingsProcessor$GuiceElementVisitor.visit(BindingsProcessor.java:820)
    at com.google.inject.internal.BindingImpl.acceptVisitor(BindingImpl.java:92)

2 个答案:

答案 0 :(得分:2)

我修好了:结果HistoryWrapper位于test/HistoryWrapperImpl位于src/test/不是GWT的源文件夹之一。糟糕。

答案 1 :(得分:0)

1)尝试将一个构造函数(即使它是空的)添加到正在注入的实现中。

2)您可能还需要尝试将@Inject添加到构造函数中,即使它是空的

我遇到了Gin注入没有构造函数的实现的问题。很奇怪但是尝试在HistoryWrapperImpl中添加一个空的construtor。如果它不起作用,请尝试将@Inject添加到构造函数...