简体   繁体   中英

Netbeans & Java - using getResource() - returning null

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM