我的代码有点问题,它总是抛出 NullPointerException
:
public class WhateverResource extends ServerResource {
@Get("json")
public Representation represent(){
InputStream is = getContext().getClass().getClassLoader().getResourceAsStream("/whatever.properties");
Properties props = new Properties();
try {
props.load(is); // NPE here!
String whatever = props.getProperty("whatever_key");
setStatus(Status.SUCCESS_OK);
} catch (IOException e) {
e.printStackTrace();
setStatus(Status.SERVER_ERROR_INTERNAL);
}
return new StringRepresentation(props.toString());
}
}
我检查了生成的 WAR 文件,在目标文件夹中,WEB-INF
文件夹下有 properties
文件。这段代码可能有什么问题?
最佳答案
答案是这样做:
InputStream is = getContext().getClass().getResourceAsStream("/whatever.properties");
并且 GAE 可以毫无问题地读取流。
没有 getClassLoader()
关于java - getResourceAsStream 总是返回 null (Google App Engine),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27717056/