ImageIO.read(getClass().getResourceAsStream returns null
已回答
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"));
请先登录再写评论。
Does the below work for you?
`getClass().getResourceAsStream(" <absolute path of the resource file> "); `
or
`getClass().getClassLoader().getResourceAsStream(fileName); `
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.
The path used by getResourceAsStream is, in fact, a URL, so try replacing the space character with
%20to try again.