Durgesh Sahu
Durgesh Sahu

Reputation: 247

Error while laoding properties file from jar file

I am trying to load a property file from within a jar file but not able do so. Following is the code of class in which i am loading the file

public class PropertyClass {
    private static Properties properties;

    public String getProperty(String propertyName)  {
        try {
            InputStream inputStream =   this.getClass().getClassLoader().getResourceAsStream("resources/test.properties");

            System.out.println("Input Stream " + inputStream);
            properties.load(inputStream);

            inputStream.close();
            if (properties == null) {
                System.out.println("Properties null");
            }
        } catch (IOException e){
            e.printStackTrace();
        }
        return properties.getProperty(propertyName);
    }
}

The class file and the property file both are packed inside the jar. But when i am trying load the file from another method it gives following error :-

Input Stream - sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@9304b1
Exception in thread "main" java.lang.NullPointerException
at PropertyClass.getProperty(PropertyClass.java:16)

It does not show input stream as null Following bit is the line 16 in my code - properties.load(inputStream);

Any help on this

Upvotes: 0

Views: 305

Answers (1)

andersschuller
andersschuller

Reputation: 13907

You need to initalise your Properties object before you can call properties.load():

private static Properties properties = new Properties();

Upvotes: 3

Related Questions