Java – inject null pointer
•
Java
We try to refactor a project with Guice The idea is to bind all language interfaces to mixed objects such as French or polish
We have a binding module:
public class StandardModule extends AbstractModule { @Override protected void configure() { bind(Language.class).to(Polish.class); } }
And a class (aboutdialog. Java) that uses this injection object
@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(); }
We also have results:
java.lang.NullPointerException at net.sf.jmoney.gui.AboutDialog.<init>(AboutDialog.java:67)
Line 67 is:
this.setTitle(language.getLanguageInUse().getString("AboutDialog.title"));
Our interface is:
public interface Language { public ResourceBundle getLanguageInUse(); }
The Polish class is:
public class Polish implements Language { private ResourceBundle languageInUse; public Polish() { languageInUse = ResourceBundle.getBundle(Constants.LANGUAGE_PL); } public ResourceBundle getLanguageInUse() { return languageInUse; } }
We're lost
Solution
I assume you didn't create your aboutdialog with Guice's help
What you can do is use inject Injectmembers (this) this is aboutdialog
The best way is to create aboutdialog by Guice, so all members will be injected
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码