Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Java: getResourceAsStream() returning null

Started by Gixugif Oct 14, 2011 at 5:40 PM 2 replies 1.8k views
Original Post
Gixugif
Gixugif
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.
6510
6510
Did you add the image folder to the classpath when running your game ?
rip-off
rip-off
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.
Gixugif
Gixugif
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.