PDA

View Full Version : class.getResourceAsStream Error



noahn567
11-04-2014, 09:58 PM
I'm using these two lines to read from a text file


InputStream ii = OBJLoader.class.getResourceAsStream(("res/" + fileName + ".obj"));
BufferedReader reader = new BufferedReader(new InputStreamReader(ii));


My error is

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at renderEngine.OBJLoader.loadObjModel(OBJLoader.java :59)
at engineTester.MainGameLoop.main(MainGameLoop.java:5 2)

I have no clue what the problem is, because the path is correct.

Agent D
11-05-2014, 03:23 AM
First of all: How is this related to OpenGL(R) and cannot be resolved in a dedicateds JAVA forum and why is reading a file considered advanced?

You have a stack trace from your exception. You could take a look at the last line in your sorce, or the very last line in the stack trace and try
to figure out what reference is null in the first place.

I don't have much Java experience (luckily), but are you sure that "OBJLoader.class.getResourceAsStream" is doing what you want it to do? Isn't
the static "class" member one of those magic reflection objects of type "Class"?

Alphaomega86
11-08-2014, 03:36 PM
Your problem is really simple to solve. You have to write:

InputStream ii = OBJLoader.class.getResourceAsStream(("/res/" + fileName + ".obj"));
BufferedReader reader = new BufferedReader(new InputStreamReader(ii));
Java wants the path to begin with a "/"!

In my opinion you should not start with OpenGL right now if you are not able to solve
such easy problems. The OpenGL error messages are not nearly as understandable
as those from Java!

@Agent D
Yes, the class keyword in Java is used for reflection.