Guice注入空指针

use*_*567 14 java guice code-injection nullpointerexception

我们尝试用Guice重构一个项目.我们的想法是将所有语言界面绑定到一个像法语波兰语这样的混合对象.

我们有一个绑定模块:

public class StandardModule extends AbstractModule {

    @Override
    protected void configure() {

       bind(Language.class).to(Polish.class);

    }
 }
Run Code Online (Sandbox Code Playgroud)

以及使用此注入对象的classe(AboutDialog.java):

@Inject Language language;

public AboutDialog(JFrame parent) {
    super(parent, "", true);
    this.language=language;
    this.setTitle(language.getLanguageInUse().getString("AboutDialog.title"));
    this.parent = parent;
    try {
        jbInit();
    } catch (Exception e) {
        e.printStackTrace();
    }
    pack();
}
Run Code Online (Sandbox Code Playgroud)

我们有结果:

java.lang.NullPointerException at net.sf.jmoney.gui.AboutDialog.<init>(AboutDialog.java:67)
Run Code Online (Sandbox Code Playgroud)

第67行是:

this.setTitle(language.getLanguageInUse().getString("AboutDialog.title"));
Run Code Online (Sandbox Code Playgroud)

我们的界面是:

public interface Language {

    public ResourceBundle getLanguageInUse();
}
Run Code Online (Sandbox Code Playgroud)

波兰语课程是:

public class Polish implements Language {

    private ResourceBundle languageInUse;

    public Polish() {
        languageInUse = ResourceBundle.getBundle(Constants.LANGUAGE_PL);
    }

    public ResourceBundle getLanguageInUse() {
        return languageInUse;
    }


}
Run Code Online (Sandbox Code Playgroud)

我们迷路了...

Jef*_*ica 10

你正在使用"现场注射".这将使得在构造函数中使用注入的值变得困难; 即使Guice要创建对象(现在没有发生)或者你要使用它injector.injectMembers(aboutDialog),构造函数也会在注入器有机会注入你想要的字段之前运行.

创建一个采用变量参数和注入参数的类有点棘手.这为您提供了一些选择:

  • 注入JFrame.如果您知道在创建构造函数时要使用的JFrame,那么只需bind(JFrame.class).toInstance(myJFrame);在模块中使用即可.然后Guice可以完全创建AboutDialog.

  • 手动创建工厂.这样你可以注入AboutDialog.Factory并只是打电话create来获得你的AboutDialog.它看起来像这样:

    public class AboutDialog extends JDialog {
    
      /** Injectable factory. */
      public static class Factory {
        @Inject private Language language;
    
        public AboutDialog create(JFrame parent) {
          return new AboutDialog(parent, language);
        }
      }
    
      // no @Inject parameter; you're calling "new" yourself above!
      public AboutDialog(JFrame parent, Language language) {
        super(parent, "", true);
        this.language = language;
        // ... other initialization
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 创建一个工厂,让Guice通过辅助注射为您打造.

    public class AboutDialog extends JDialog {
    
      public interface Factory {
        public AboutDialog create(JFrame parent);
      }
    
      // you need the @Inject, and also the @Assisted to tell Guice to
      // use the parameter instead of Guice bindings
      @Inject
      public AboutDialog(@Assisted JFrame parent, Language language) {
        super(parent, "", true);
        this.language = language;
        // ... other initialization
      }
    }
    
    public class StandardModule extends AbstractModule {
      @Override protected void configure() {
        bind(Language.class).to(Polish.class);
    
        // here every method in AboutDialog.Factory will be implemented
        // to create the method's return type [AboutDialog] based on
        // the parameters (like JFrame) and the bindings (like Language)
        install(new FactoryModuleBuilder().build(AboutDialog.Factory.class));
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)

正如问题评论中所述,确保你得到你的AboutDialog(或AboutDialog.Factory通过@Injected构造函数/字段或来自它Injector自己,否则Guice将不知道注入参数.

  • 这句话“构造函数将在注入器有机会注入你想要的字段之前运行”拯救了我的一天,因为我不明白为什么我的默认构造函数中使用的注入字段总是为空。谢谢!!! (2认同)

小智 8

我假设你不是AboutDialog在Guice的帮助下创造你的.

你可以做的就是使用injector.injectMembers(this)其中this的是AboutDialog.

最好的方法是AboutDialog由Guice创建,所以所有成员都将被注入.