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

Show parent comments

1

u/Born-Ad4658 2d ago

1

u/KillerCodeMonky 2d ago

So in ImagesLoader, you set IMAGE_DIR = "Images/". Since this is a relative path, the Class.getResource method will prepend it with the class' package. Now your classes are not actually in a package, so I'm not sure how that interacts. However, the first thing I would try is instead use:

    IMAGE_DIR = "/Images/"

Note the extra / in the front. This makes it an absolute resource path and it will look for Images as the class path root.

1

u/Born-Ad4658 2d ago

That did not work.

I right clicked the folder went to Properties > Java Build Path and every thing is listed.

Errors I'm getting:

Linking the MIDI sequencer and synthesizer

Problem with Sounds/Mission_Impossible.mid

-- mi/Mission_Impossible.mid

Reading file: /Images/imsInfo.txt

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)

at ImagesLoader.loadImagesFile(ImagesLoader.java:97)

at ImagesLoader.<init>(ImagesLoader.java:66)

at AlienTilesPanel.<init>(AlienTilesPanel.java:109)

at AlienTiles.<init>(AlienTiles.java:73)

at AlienTiles.main(AlienTiles.java:114)

I will research and look at this more.

1

u/KillerCodeMonky 2d ago

I'm not sure how much more I'll be able to help, because you're using an unconventional project structure and no project definition file. I'm guessing you're directly defining this project in Eclipse or IntelliJ. A typical Maven project would your have classes under src/main/java and your resources under src/main/resources. But as is I can't reliably recreate your environment.

1

u/Born-Ad4658 2d ago

Is Maven a necessity  for most projects?

1

u/KillerCodeMonky 2d ago

If you want your project to reliably be worked on by others, then yes: Having a well known, common format for defining the project is a necessity. Maven and Gradle are the most popular by far.

I'm not saying you need to use them, even though it would take a grand total of about 5 minutes to reorganize your project for it. But I am saying that myself, from outside your computer, cannot reliably tell you anything more about why your project is not able to resolve resources, because I have no way to reproduce how you are defining that project and its resources.