在 JavaEE 中使用 @Inject 注解时出现 NullPointerException
NullPointerException when using @Inject Annotation in JavaEE
我有以下服务class:
@Singleton
public class QuotesLoaderBean {
Properties quotes;
Properties names;
@Inject
public QuoteRepository repo;
public QuotesLoaderBean() {
}
@PostConstruct
public void init() {
InputStream quotesInput = this.getClass().getClassLoader().getResourceAsStream("quotes.properties");
InputStream namesInput = this.getClass().getClassLoader().getResourceAsStream("names.properties");
quotes = new Properties();
names = new Properties();
try {
quotes.load(quotesInput);
names.load(namesInput);
} catch (IOException ex) {
Logger.getLogger(QuotesLoaderBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Citation createCitation(String quote) {
Citation citation = new Citation();
citation.setQuote(quote);
citation.setWho(getName());
repo.save();
return citation;
}
public Citation getCitation() {
Citation citation = new Citation();
citation.setQuote(getQuote());
citation.setWho(getName());
return citation;
}
public String getQuote() {
Enumeration keys = quotes.propertyNames();
int elementNumber = new Random().nextInt(quotes.keySet().size());
return quotes.getProperty(getElement(keys, elementNumber));
}
public String getName() {
Enumeration keys = names.propertyNames();
int elementNumber = new Random().nextInt(names.keySet().size());
return names.getProperty(getElement(keys, elementNumber));
}
private String getElement(Enumeration keys, int elementNumber) {
int i = 0;
while (keys.hasMoreElements()) {
if (i == elementNumber) {
return (String) keys.nextElement();
} else {
i++;
keys.nextElement();
}
}
return null;
}
}
存储库 class 对于测试目的非常简单:
@Singleton
public class QuoteRepository {
public String save() {
Gson gson = new GsonBuilder().create();
return "Saved...";
}
}
当我测试 createCitation 方法时,我总是得到 NullPointerException,但我不知道为什么。某些东西不适用于注入。我还有一个 api class 用 @Stateless 注释,在那里我可以很容易地用 @Inject 注释注入服务 class。
When I test the createCitation method I always get a NullPointerException
您不能简单地测试您的应用程序,因为您将创建对象的责任委派给了在单元测试中(我假设您使用它)不存在的容器。
public Citation createCitation(String quote) {
Citation citation = new Citation();
citation.setQuote(quote);
citation.setWho(getName());
repo.save(); // repo isn't initialized
return citation;
}
如果您想测试您的代码,请模拟 repo
对象或使用集成测试。
我有以下服务class:
@Singleton
public class QuotesLoaderBean {
Properties quotes;
Properties names;
@Inject
public QuoteRepository repo;
public QuotesLoaderBean() {
}
@PostConstruct
public void init() {
InputStream quotesInput = this.getClass().getClassLoader().getResourceAsStream("quotes.properties");
InputStream namesInput = this.getClass().getClassLoader().getResourceAsStream("names.properties");
quotes = new Properties();
names = new Properties();
try {
quotes.load(quotesInput);
names.load(namesInput);
} catch (IOException ex) {
Logger.getLogger(QuotesLoaderBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Citation createCitation(String quote) {
Citation citation = new Citation();
citation.setQuote(quote);
citation.setWho(getName());
repo.save();
return citation;
}
public Citation getCitation() {
Citation citation = new Citation();
citation.setQuote(getQuote());
citation.setWho(getName());
return citation;
}
public String getQuote() {
Enumeration keys = quotes.propertyNames();
int elementNumber = new Random().nextInt(quotes.keySet().size());
return quotes.getProperty(getElement(keys, elementNumber));
}
public String getName() {
Enumeration keys = names.propertyNames();
int elementNumber = new Random().nextInt(names.keySet().size());
return names.getProperty(getElement(keys, elementNumber));
}
private String getElement(Enumeration keys, int elementNumber) {
int i = 0;
while (keys.hasMoreElements()) {
if (i == elementNumber) {
return (String) keys.nextElement();
} else {
i++;
keys.nextElement();
}
}
return null;
}
}
存储库 class 对于测试目的非常简单:
@Singleton
public class QuoteRepository {
public String save() {
Gson gson = new GsonBuilder().create();
return "Saved...";
}
}
当我测试 createCitation 方法时,我总是得到 NullPointerException,但我不知道为什么。某些东西不适用于注入。我还有一个 api class 用 @Stateless 注释,在那里我可以很容易地用 @Inject 注释注入服务 class。
When I test the createCitation method I always get a NullPointerException
您不能简单地测试您的应用程序,因为您将创建对象的责任委派给了在单元测试中(我假设您使用它)不存在的容器。
public Citation createCitation(String quote) {
Citation citation = new Citation();
citation.setQuote(quote);
citation.setWho(getName());
repo.save(); // repo isn't initialized
return citation;
}
如果您想测试您的代码,请模拟 repo
对象或使用集成测试。