4

I have seen this posted a couple of times and tried a few of the suggestions with no success (so far). I have a maven project and my properties file in on the following path:

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

I am trying to load it in a Singleton class to access the properties by key

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);
    }

}

but I am getting a NullPointerError when this runs... I have done everything I can think of to the path, but it won't find/load the file.

Any ideas?

Stacktrace:

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)
Thomas Moorer
  • 73
  • 1
  • 2
  • 6
  • 1
    Please add the `stackstrace` and point us to that line where its throwing the `exception`. – Smit Jul 25 '14 at 18:31
  • i would question the structure of your properties file.. the stacktrace error is not coming from your code – T McKeown Jul 25 '14 at 18:42

2 Answers2

5

You should instantiate your Properties object. Also you should load the resource file with the path starting with /META-INF:

properties = new Properties();
properties.load(getClass().getResourceAsStream("/META-INF/testing.properties"));
M A
  • 71,713
  • 13
  • 134
  • 174
1

properties is null... you must first instantiate it.. then load it.

T McKeown
  • 12,971
  • 1
  • 25
  • 32