• Advertisement
Sign in to follow this  

[java] Java: getResourceAsStream() returning null

This topic is 2364 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"));


The file getResourceAsStream is looking at definitely exists (it's a variable in the actual code, but I figure simpler to write it out here), but for whatever reason, "is" is null on the next line. I'm guessing there's something I don't understand about how this works, but anything I've read so far on the matter hasn't been at all helpful, so I would be thankful for anyone who can explain to me what's going on.

I'm sure it's not an issue of where the file is located. My classes are in /src in the DreamGame folder, and as you can see, Images is in the Images folder.

Share this post


Link to post
Share on other sites
Advertisement
Did you add the image folder to the classpath when running your game ?

Share this post


Link to post
Share on other sites
If you want to directly load a file, you can use a FileInputStream. Using getResourceAsStream is different from simply loading a file, as 6510 suggests.

Share this post


Link to post
Share on other sites
Oops, sorry, took a while for me to get back to this. Edited the line in the op a little bit, but shouldn't have made a functional difference.

So the problem was it was looking for "/Images" in the bin directory when the contents of "/Images" were in bin, but not "/Images" itself. Changing the code to read "/player.gif" instead of "/Images/player.gif" fixed this.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement