ImageIO.read(getClass().getResourceAsStream returns null

Answered

I feel like I have tried every single path and I have no idea what is wrong I have it in a player directory connected to a res then the main file and I'm trying to call it Src Main.

 

Here is the code that isn't working: 

ImageIO.read(getClass().getResourceAsStream("/player/Boy_Up_1.png"));
0
3 comments

Does the below work for you?

`getClass().getResourceAsStream(" <absolute path of the resource file> "); `

or

`getClass().getClassLoader().getResourceAsStream(fileName); `

0

Hi!!

 I suppose that you where following the Riysnow java tutorial.  I recently ran into this problem while refactoring the code after finishing the first part. The new method for scaling the player  threw a NullPointerException and after looking deeply the problem wasn't in the setup method, but in the getPlayerImage() where I left a blank space in one of the image names.

 Maybe that's what happened to you.

Hope this helps.

0

The path used by getResourceAsStream is, in fact, a URL, so try replacing the space character with %20 to try again.

0

Please sign in to leave a comment.