在Singleton类中加载属性文件

我已经看过几次,并尝试了一些没有成功的建议(到目前为止)。 我有一个maven项目,我的属性文件位于以下路径:

[project]/src/main/reources/META_INF/testing.properties 

我试图在Singleton类中加载它以按键访问属性

 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) 

您应该实例化您的Properties对象。 您还应该使用以/META-INF开头的路径加载资源文件:

 properties = new Properties(); properties.load(getClass().getResourceAsStream("/META-INF/testing.properties")); 

properties为null …您必须首先实例化它然后加载它。