views:

27

answers:

2

Hi! I am working in a project and I need a resource in it. I am trying to get it using this code:

InputStream is = JSONParser.class.getResourceAsStream("a.json");
String jsonTxt = IOUtils.toString( is );

The file a.json is located in the main file of the project.

The exact problem is:

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.(Reader.java:61)
at java.io.InputStreamReader.(InputStreamReader.java:55)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1049)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:359)
at com.playence.parser.JSon.main(JSon.java:28)

Debugging, I saw that variable 'is' is null, and I guess that is why NullPointException, bugt I don't know how to solve it.

Any help?

Thans in advance.

+1  A: 

You have the exception because a.json can't be found by your Class.getResourceAsStream() in which case this returns null instead of the stream.

Your a.json file should be in the same package as the JSONParser class the way you have it now. Is that one of your classes or is a utils from an external library?

If it is a library, maybe you can retrieve the stream from a class that is in the same package as a.json and then pass it as parameter:

InputStream is = YourClassBesidesTheFile.class.getResourceAsStream("a.json");
String jsonTxt = IOUtils.toString( is );

or maybe use a full path for the file, something like "/some/package/etc/etc/a.json":

InputStream is = JSONParser.class.getResourceAsStream("/some/package/etc/etc/a.json");
String jsonTxt = IOUtils.toString( is );

To solve the issue, your file must be found by the Class.getResourceAsStream() method although the real issue is in fact retrieving the file content as a stream, no matter how you do that.

dpb
I put the file in src/main/resorces, and also Y tryed putting it into the same file that the class which is is calling it, but I get the same error.Typing the hole path is also the same result.Maybe could it be a problem with ecplipse and libraries??
mujer esponja
By the way, I am using JSONParser by importing maven dependencies, I have not JSONParser.java in my project.
mujer esponja
@mujer-esponja: I see you are using src/main/resorces folder. Are you using Maven?
dpb
yes, using Maven
mujer esponja
@mujer-esponja: the files you place in src/main/resorces are placed exactly as is in your jar (I assume you are building a jar or using a maven project, see here: http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR). You can add the file with a sub-package path the same as the class that reads it and then retrieve it as such (like in my example) or I guess that JSONParser.class.getResourceAsStream("/a.json") should work. It depends on your environment.
dpb
A: 

Finally I found my problem and I solved it.
It was related with my Eclipse configuration:

Project - propperties - Source

There I had in Myproject/src/main/resources Exclude **.

So the files in that directory wasn't in classes. I solved it just putting Exclude (None).

Now everything is ok. Thank you anyway!

mujer esponja