[英]getResources() returns null
Using Eclipse IDE. 使用Eclipse IDE。 The line: 该行:
getClass().getResource("/res/bitmaps/image.png");
returns null
. 返回null
。 I have created the res
folder in the root of my project. 我在项目的根目录下创建了res
文件夹。
The code of interest is: 感兴趣的代码是:
bImage = ImageIO.read(getClass().getResource("/res/bitmaps/image.png"));
and it throws the exception: 并引发异常:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1378)
at com.example.game.resource.Resources._loadImage(Resources.java:31)
at com.example.game.GameComponent.<init>(GameComponent.java:19)
at com.example.game.GameFrame.<init>(GameFrame.java:8)
at com.example.game.GameFrame.main(GameFrame.java:13)
Any help? 有什么帮助吗?
ImageIO.read(getClass().getResourceAsStream("res/drawable/image.png"));
Make sure res folder is in class path, verify using project properties > Java build Path > Source tab. 确保res文件夹在类路径中,使用项目属性> Java构建路径>源选项卡进行验证。 If not in class path, can add via Add Folder.. button on the right. 如果不在类路径中,可以通过右侧的“ 添加文件夹..”按钮添加 。
You say the resource is in "the root of my project" - is it that folder in your build path? 您说资源在“我的项目的根目录”中-是构建路径中的那个文件夹吗? You need to have it in your build path so that Eclipse will copy it to the output directory ( bin
by default). 您需要将其放在构建路径中,以便Eclipse将其复制到输出目录(默认情况下为bin
)。
getClass().getResource("/res/drawable/image.png");
您应提供其中存在image.png的文件夹的路径
If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'. 如果名称以'/'('\\ u002f')开头,则资源的绝对名称是名称中'/'之后的部分。
Otherwise, the absolute name is of the following form: package_name/name 否则,绝对名称的格式如下:package_name / name
Where the package_name is the package name of this object with '/' substituted for '.' 其中package_name是此对象的软件包名称,其中用'/'代替'。'。 ('\.'). ('\\ u002e')。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.