I am using the following code to read a properties file:
Properties pro = new Properties();
InputStream is = Thread.currentThread().getContextClassLoader().
getResourceAsStream("resources.properties");
pro.load(is);
And when I execute the code I'm getting the following error:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at com.ibm.rqm.integration.RQMUrlUtility.RQMRestClient.getResource(RQMRestClient.java:66)
at com.ibm.rqm.integration.RQMUrlUtility.RQMRestClient.main(RQMRestClient.java:50)
Why am I getting a NullPointerException
? And where should I save the resources.properties
file?
It looks like
ClassLoader.getResourceAsStream(String name)
returnsnull
, which then causesProperties.load
to throwNullPointerException
.Here's an excerpt from documentation:
See also
getResource
Well it depends; As per javadoc ... The context ClassLoader is provided by the creator of the thread for use by code running in this thread when loading classes and resources. If not set, the default is the ClassLoader context of the parent Thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load the application...
So if
Thread.currentThread().getContextClassLoader()
is in the main() function and you haven't created any thread then it should have the same package as that of the class containing method main. Otherwise it should be present in the class which has created the thread....Many seem to have this problem and like me they give up after sometime. Here is what I had to get this working. The trick here to use relative path for file lookup is to make sure your classes folder contains resources files along with src files. Here is what I ended up doing.
1) If you are using eclipse make sure you proper .classpath setting present and do PROJECT CLEAN to see the resources files get generated under /classes. Notice the classpath-entries below for resource files place under src/main/resource
2) If you are using maven as well make sure you configure your pom.xml as per the https://maven.apache.org/guides/introduction/introduction-to-the-pom.html and do mvn clean install to see the files under target/classes
3) Once you have got the resource files under /classes the next thing to do in java is the following. Don't forget to have the forward slash.
I could have added some images but did not have points. :)
I had the same problem and I have resolved by doing the following
File file = new File("resources.properties");
System.out.println(file.getAbsolutePath());
and then put "resources.properties" file under that path.
Perhaps, I have plucked all my hairs out and going then I have found this solution out:
I had the same problem and was quite confused as I used it previously in a Sturts application. But the problem was that I didn't understand the type of ClassLoader that Struts returns is different than what Spring returns. And the way i figured it out was i printed out the object that was returned on to the system console like this:
[
WebappClassLoader
context: /MyProject
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@1004901
]
It gave me the detail of the object, and in that I found its type to be of WebAppClassLoader which will start looking for files in the WEB-INF/classes/ folder after a build is done. So I went into the that folder and looked for where my file is located so I gave the path accordingly.
In my case it was located in /WEB-INF/classes/META-INF/spring/filename.extension
Voilà!
That fixed it all.