Guice 注入空指针
2022-09-03 01:22:42
我们尝试使用 Guice 重构项目。这个想法是将所有语言界面绑定到一个创建对象,如法语或波兰语。
我们有一个用于绑定的模块:
public class StandardModule extends AbstractModule {
@Override
protected void configure() {
bind(Language.class).to(Polish.class);
}
}
以及使用此注入对象的类(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();
}
结果:
java.lang.NullPointerException at net.sf.jmoney.gui.AboutDialog.<init>(AboutDialog.java:67)
第 67 行是:
this.setTitle(language.getLanguageInUse().getString("AboutDialog.title"));
我们的界面是:
public interface Language {
public ResourceBundle getLanguageInUse();
}
波兰语类是:
public class Polish implements Language {
private ResourceBundle languageInUse;
public Polish() {
languageInUse = ResourceBundle.getBundle(Constants.LANGUAGE_PL);
}
public ResourceBundle getLanguageInUse() {
return languageInUse;
}
}
我们迷失了...