1

I am getting null pointer exception on using InputStream along with class Loader function but on using FileInputStream it is reading the properties file correctly.

Why I am getting this error? Given below is my code.

public String readProperties()
{

    String result = "";
    Properties prop = new Properties();
    String file = "test.properties";
    //InputStream fins = getClass().getClassLoader().getResourceAsStream(file); 
    try 
    {
        prop.load(new FileInputStream(file));
        //prop.load(fins);
    } 
    catch (IOException e) {
        e.printStackTrace();
    }

    String nation = prop.getProperty("Nation");
    String city = prop.getProperty("City");
    String state = prop.getProperty("State");
    result = "I live in "+city+" in "+state+" in "+nation;
    return result;
}
Deepak Dave
  • 47
  • 1
  • 3
  • 13
  • 2
    So the code you've shown *doesn't* give an error? That's quite a confusing way of asking the question. If the problem is when you're loading it as a resource, we'll need to know more about how you expect the resource to be present at execution time - is this loading from a jar file, or just classes? Is your resource being copied into the classpath? Is your code in a package? – Jon Skeet Nov 17 '14 at 07:13
  • My test.properties file is present in the root directory of my project alongside src and bin folders. It is being loaded from classes. Path of my code is "TestNG_trial"->src->"AdactIn_SH"->"PropertiesDemo.java". I have tried some other suggestions given but still the NullPointerException is coming. I also tried to set the path of the properties file in the Environment Variables in PATH as well as CLASSPATH in System Variables. Can't understand why it is not able to read the properties file. – Deepak Dave Nov 24 '14 at 04:54

3 Answers3

8

Make sure that you kept your test.properties file in the classpath : i.e in Src folder of your application

here is the Sample Code :

package com.example;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadProperties {

    public static void main(String[] args) {        

        ReadProperties r = new ReadProperties();        
        String  result = r.readProperties();
        System.out.println("Result   : " + result);
    }

    public String readProperties()
    {
        String result = "";
        Properties prop = new Properties();
        String file = "test.properties";
        InputStream fins = getClass().getClassLoader().getResourceAsStream(file); 
        try 
        {
            //prop.load(new FileInputStream(file));
            if(fins!=null)
                prop.load(fins);        
        } 
        catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }

        String nation = prop.getProperty("Nation");
        String city = prop.getProperty("City");
        String state = prop.getProperty("State");
        result = "I live in "+city+" in "+state+" in "+nation;
        return result;
    }

}
Yog
  • 81
  • 1
2

The getResourceAsStream-method will in your case search in the package of your class.

From ClassLoad.getResource:

This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.

These ClassLoader-methods are for searching the probably bundled java application (e.g. as jar file) and not for searching files beside your application, which seems, what you want to do in this case. See ClassLoader JavaDoc.

If the ClassLoader is unable to find the resource, the getResource*-methods will return null and hence your code will fail (NullPointerException -> the stream is null).

Update: If the properties-file is in the root of your project, you might try it with a / at the beginning of the path, when using the ClassLoader. See this thread for further information.

Community
  • 1
  • 1
TobiasMende
  • 741
  • 3
  • 8
0

The code could be like this:

String file = "/test.properties";
InputStream fins = getClass().getResourceAsStream(file); 
InputStream fins = MyClass.class.getResourceAsStream(file); 

The resource is sought relative to the class of getClass(), but is made absolute with a starting /. Using getClass() however means that the actual class may be from another jar, and thus it might be better to use the actual class name.

In contrast to using File, a resource might be taken from a jar (a zip format). As File on Windows is case-insensitive, one might have Test.properties working on Windows using File, and not as resource or other platforms (Linux, MacOSX).

Open the jar (zip) and check that test.properties is there.

For completeness sake: you can also use a ClassLoader to fetch the resource. This is nice across jar. The path then however has to be absolute, not starting with /.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138