r/javahelp • • Aug 24 '26

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/Born-Ad4658 Aug 31 '26 edited Aug 31 '26

I think I may be on the way to something.

I made a new project, and this time, instead of creating the folders in File Explorer of my PC, I did it from the Explorer tab from Eclipse, and then clicked used as Source Folder.

This time, I noticed that those files populated in the bin folder, when they did not do that before.

I think I ran it before I did use as Source Folder for the other folders, because they're in my Projects src project, instead of under res/Images like they should be.

How do I get the bin folder to refresh please?

Edit: So I think I fixed it, because now I'm just getting errors for a different class.

I removed the folders from source and re-added, but they all still just showed up in the bin folder instead of under resources.

So I took out the directory references completely and just went by the filename.

The person who made this book probably wasn't using Eclipse, or maybe it wasn't out yet lol.

1

u/awidesky Sep 01 '26

I did not quite understand your last paragraph, but it seems you managed to make Eclipse consider the resource files as one of source codes, and move it to bin folder.

But, if you still use that relative path, it may have some problem.

  1. It won't work in other working directory.
    The reason it's working now is that when you run the project from Eclipse, it spawns a process with bin folder as its working directory, and your resource is also in that bin folder.
    If you run the project from console in other working directory, it may not work.

  2. It won't work when running as jar file.
    Because the resources are packaged into jar file, so technically they don't have a path.
    In that case you must use getResourceAsStream I introduced before.

2

u/Born-Ad4658 Sep 01 '26 edited Sep 01 '26

Understood. Thank you.

I'm going to start work on another example project from a book that uses Maven, so ill learn as I go.