Question

I am trying to load a background to my JFrame using the following code:

image = ImageIO.read(getClass().getResourceAsStream(s));

where for s I have tried:

/res/Background/bg_menu.gif
Background/bg_menu.gif
/Background/bg_menu.gif
res/Background/bg_menu.gif

My res folder is in the project root like so:

Game
-- src
-- res

I have done the following:

Project Properties -> Sources -> Add Folder -> res

The error I'm getting returned is:

java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at com.game.rpg.tilemap.Background.<init>(Background.java:29)
at com.game.rpg.gamestate.MenuState.<init>(MenuState.java:34)
Was it helpful?

Solution

If this is your package structure

/res/Background/bg_menu.gif

and /res is a source folder, then

/Background/bg_menu.gif

should be at the root of your classpath. As such, you can access it with

image = ImageIO.read(getClass().getResourceAsStream("/Background/bg_menu.gif"));

Note the leading /. The rules for the path are explained in the javadoc.

If this doesn't work, then your application is not being built correctly with Netbeans. check the deployment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top