我正在尝试从文件 (test.properties) 加载属性
我使用的代码如下:
URL url = getClass().getResource("../resources/test.properties");
properties.load(url.openStream());
但是当执行第二行时,我得到了一个 NPE。 (空指针异常)
我不确定这里出了什么问题...我已检查该文件存在于 URL 指向的位置...
感谢任何帮助....
最佳答案
Class.getResource(String)
的 javadoc说:
Returns: a URL object or null if no resource with this name is found
问题很可能是 getResource 没有找到它正在寻找的资源。我非常怀疑在资源名称中使用 ".."
。 javadoc 没有说 getResource
将 "."
或 ".."
路径组件视为具有特殊含义。
也有可能properties
为null ...
关于java - 加载 java 属性时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2698532/