developer
developer

Reputation: 9478

getting null pointer when reading the properties file

when reading the properties file, iam getting nullpointer exception.

faceConfig.load(ReadPropertyFile.class.getClassLoader().getResourceAsStream("/resources/faces.properties"));

below is the path of properties file facedetections/src/main/resources/faces.properties

i tried in different combinations as my class file that reads in below path /facedetections/src/main/java/com/facial/facedetection/utils/ReadPropertyFile.java

combinations are ../../../../../resource/faces.properties , /resource/faces.properties and

../../../resource/faces.properties

Please suggest what is the correct path i can provide for this.

Edit : I extracted the war file and providing its path below. war extraction

Upvotes: 0

Views: 925

Answers (3)

Chamly Idunil
Chamly Idunil

Reputation: 1872

looks your property file under class folder not in resource. as your screen shot cant find resource folder under class folder.

So just use

 ReadPropertyFile.class.getClassLoader().getResourceAsStream("faces.properties")

Upvotes: 2

Henry
Henry

Reputation: 43728

Since it is unlikely that you get a NPE when the file is not found I assume that faceConfig is null when you execute that line.

Upvotes: 0

Mario Rossi
Mario Rossi

Reputation: 7799

The path is relative to the point where the object (.class) files are located. Are you sure you have configured your build/test tool to copy the resource file into that structure? Exactly where? That's what counts, not the position of the sources.

Additionally, my understanding is that getResourceAsStream() of most ClassLoaders do not support .. notation.

The position where your resource file is currently located is out of reach of the ClassLoader. If you move your file to /facedetections/src/main/java/resources/faces.properties, then you will be able to use the current code

getResourceAsStream("resources/faces.properties")

I'm making assumptions about your environment. In particular, this is entirely dependent on classloaders. If this doesn't help, please provide object file location, not sources (unless it is the same, but state it).

Upvotes: 0

Related Questions