【发布时间】:2014-09-17 16:39:19
【问题描述】:
我已经看过这个帖子几次,并尝试了一些建议但没有成功(到目前为止)。我在以下路径中有一个 Maven 项目和我的属性文件:
[project]/src/main/reources/META_INF/testing.properties
我正在尝试将其加载到单例类中以通过键访问属性
public class TestDataProperties {
private static TestDataProperties instance = null;
private Properties properties;
protected TestDataProperties() throws IOException{
properties = new Properties();
properties.load(getClass().getResourceAsStream("testing.properties"));
}
public static TestDataProperties getInstance() {
if(instance == null) {
try {
instance = new TestDataProperties();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return instance;
}
public String getValue(String key) {
return properties.getProperty(key);
}
}
但是当它运行时我得到一个 NullPointerError...我已经对路径做了我能想到的所有事情,但它不会找到/加载文件。
有什么想法吗?
堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
【问题讨论】:
-
请添加
stackstrace并将我们指向它抛出exception的那一行。 -
我会质疑你的属性文件的结构。堆栈跟踪错误不是来自你的代码
标签: java maven properties classpath