Netbeans & Java - using getResource () - return null

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

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

where for si tried:

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

My res folder is at the root of the project like this:

Game
-- src
-- res

I have done the following:

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

The error I am getting 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)
0
source share
1 answer

If this is your package structure

/res/Background/bg_menu.gif

and /resis the source folder then

/Background/bg_menu.gif

should be at the root of your class path. This way you can access it using

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

Pay attention to the beginning /. Rules for the path are explained in javadoc.

, Netbeans. .

+2

All Articles