r/javahelp 8d ago

Unsolved I need help with getResourceAsStream

u/wildjokers (reaching out to you because I saw you in this thread)

https://www.reddit.com/r/javahelp/comments/sypxqz/getresourceasstream_null_when_run_from_jar/

I'm trying to run a Isometric Tile game, from chapter 13 of Killer Games Programming in Java, a very old book.
I'm using Eclipse. I'm not sure what classpath means, but I think mine is set up as

Project

src

Images

Sounds

I'm getting these errors when I run:

Exception in thread "main" java.lang.NullPointerException

at java.base/java.io.Reader.<init>(Reader.java:168)

at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:88)

The relevant code is:

{

String imsFNm = IMAGE_DIR + fnm;

System.out.println("Reading file: " + imsFNm);

try {

InputStream in = this.getClass().getResourceAsStream(imsFNm);

BufferedReader br = new BufferedReader( new InputStreamReader(in));

// BufferedReader br = new BufferedReader( new FileReader(imsFNm));

String line;

The image file is defined in another class, and then piped into ImageLoader class, where it's appended to the directory, which is defined in ImageLoader class.

When I run, it's showing the correct directory in the console, so I believe getResourceAsStream is where it's being messed up.

I did right click on the images folder, Build Path > Add to Build Path.

I can provide more information, which will probably be needed.

2 Upvotes

28 comments sorted by

View all comments

1

u/ChaiTRex 8d ago

You should look at the stack trace and go down from the top until you see a file that you wrote. The stack trace you showed here doesn't help.

1

u/Born-Ad4658 2d ago

1

u/ChaiTRex 2d ago edited 2d ago

No, I mean, in order to get better at debugging your code, you should personally look at the stack trace because it can really help you.

You said that you got a stack trace like:

Exception in thread "main" java.lang.NullPointerException
    at java.base/java.io.Reader.<init>(Reader.java:168)
    at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:88)

That's only the beginning of the stack trace. Reader.java and InputStreamReader.java in those lines are classes that you didn't write. You should keep going through the lines until you see the first line that is a class that you wrote.

You'll see something like MyProject.java:33. This tells you that line 33 in MyProject.java caused the error. Since it's a NullPointerException, that means you had a method call or field access on an object that was set to null or that you passed a variable set to null into a method that expects something that's not null.

So, for example, if you did InputStreamReader reader = new InputStreamReader(myInputStream);, myInputStream is probably set to null for some reason.

1

u/Born-Ad4658 2d ago

I know the line.

I also just threw in the a throw illegalargumentexception and confirmed that the inputstream is coming up null. 

Its not finding the file

I think my next step is seeing if I can get this to run

https://howtodoinjava.com/java/io/read-file-from-resources-folder/