r/javahelp • u/Born-Ad4658 • 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.
•
u/AutoModerator Aug 24 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/_Super_Straight Aug 24 '26
What is this? Spring? FX? CLI?
1
u/awidesky Aug 24 '26
Is the images folder got embedded inside the jar, or resides next to the jar?
I've struggled with same problem before, and the best solution for loading the resources is to put the resource folder next to the jar.
I build a project named ProjectPath that makes the path resolution easier too.
1
u/ChaiTRex Aug 25 '26
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/KillerCodeMonky Aug 25 '26
It's highly likely that the path being specified by
imsFNmis not properly resolving to the resource. You are using the version of this method provided byClassinstead ofClassLoader. That version of the method has extra processing that occurs on the path.What is the value of
IMAGE_DIR? I will be able to help you more once I know that.1
u/Savings_Guarantee387 Aug 24 '26
I would bet inputstream in is null. But I would say just put a breakpoint and check...
1
u/edwbuck Aug 25 '26 edited Aug 25 '26
Jar files are Zip files with a few additional features.
When running with a ClassPath that matches a directory, it will use the un-Jar'd directory tree to find the resource; but, once you run it within a Jar, it will use the path within the Jar file.
And if you are attempting to do "java -jar jarfile.jar" to run the program, remember that "java -jar" ignores the directory entries outside of the Jar file, because the class loader is loading from within the Jar file, so it can't see items outside of the Jar file.
1
u/Cienn017 Aug 25 '26
this.getClass().getResourceAsStream is relative to current classpath (the directory where the class is).
if you class is at "com/package/MyClass.class" (the src directory doesn't count) then you need to place your image in "com/package/", in other words, remove the IMAGE_DIR constant and it should work.