编程语言
首页 > 编程语言> > java – getResourceAsStream总是返回null(Google App Engine)

java – getResourceAsStream总是返回null(Google App Engine)

作者:互联网

我的代码有点问题,它总是抛出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文件夹下有该属性文件.这段代码有什么问题?

解决方法:

答案是这样做:

    InputStream is =  getContext().getClass().getResourceAsStream("/whatever.properties");

并且GAE可以毫无问题地读取流.

没有getClassLoader()

标签:java,google-app-engine,restlet
来源: https://codeday.me/bug/20190528/1171836.html