Netbeans& Java-使用getResource()-返回null [英] Netbeans & Java - using getResource() - returning null

查看:100
本文介绍了Netbeans& Java-使用getResource()-返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将背景加载到我的JFrame中:

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

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

我尝试过的s的位置:

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

我的res文件夹位于项目根目录中,如下所示:

My res folder is in the project root like so:

Game
-- src
-- res

我已经执行以下操作:

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

我得到的错误是:

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)

推荐答案

如果这是您的包结构

/res/Background/bg_menu.gif

/res是源文件夹,然后

/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"));

请注意开头的/. 路径在javadoc中进行了解释.

如果这不起作用,则说明您的应用程序未使用Netbeans正确构建.检查部署.

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

这篇关于Netbeans&amp; Java-使用getResource()-返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆