尝试使用 ImageIO.read(class.getResource(URL)) 加载图像但 getResource 返回 null [英] Trying to load image using ImageIO.read(class.getResource(URL)) but getResource is returning null
问题描述
我一直在和我的好友一起制作 2D 游戏,并且通过一些 Youtube 教程学到了很多关于一些基本游戏开发概念的知识.我正在学习的一件事是精灵(对于那些不知道的人,要渲染到屏幕上的 2D 图像)以及如何在我的游戏中使用它们.我一直在使用 ImageIO.read(this.class.getResource(pathToMySprite))
但似乎 getResource()
正在为 null
返回某种原因.
I've been making a 2D game with my buddy and I've been learning a lot about some basic game dev concepts through some Youtube tutorials. One of the things I was learning about is sprites (for those that don't know, 2D images to render to the screen) and how to use them in my game. I've been using ImageIO.read(this.class.getResource(pathToMySprite))
but it seems that getResource()
is returning null
for some reason.
我一直在修改路径,在它前面添加/",删除/",放置 user.dir 属性以查看它是否需要整个路径,我仍然得到同样的错误.
I've been screwing around with the path a little, adding "/" in front of it, removing "/", putting the user.dir property to see if it needed the whole path, and I'm still getting the same error.
TILE_TEXTURES(System.getProperty("user.dir") + "/textures/tile.png");
//ENTITY_TEXTURES("/textures/entity.png");
private BufferedImage img;
private SpriteSheet(String path) {
System.out.println(System.getProperty("user.dir"));
try {
//TODO: Fix this error, don't know what's wrong.
img = ImageIO.read(SpriteSheet.class.getResource(path)); // error here!!!
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getImage() {
return img;
}
感谢任何和所有帮助.我没有评论代码(当我到达可以坐下来对我完成的事情感到满意的地方时,我通常会这样做)但这是一个非常小的课程,所以我想你们将能够理解什么是一切顺利.
Any and all help is appreciated. I haven't been commenting the code (I usually do that when I get to place where I can sit back and be happy with what I've finished) but it's a pretty small class so I think you guys will be able to understand what's going on just fine.
在我的项目的类路径中保存图像的文件夹IS.我还包括错误:
The folder that holds the image IS in the class path of my project. I've also included the error:
Exception in thread "Thread-2" java.lang.ExceptionInInitializerError
at com.brickbattle.client.src.gui.Sprite.<clinit>(Sprite.java:7)
at com.brickbattle.client.src.objs.Tile.<init>(Tile.java:67)
at com.brickbattle.client.src.objs.Player.initPlayerNum(Player.java:19)
at com.brickbattle.client.src.util.BrickBattle.init(BrickBattle.java:114)
at com.brickbattle.client.src.util.BrickBattle.run(BrickBattle.java:85)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: input == null! //HERE IS ERROR
at javax.imageio.ImageIO.read(Unknown Source)
at com.brickbattle.client.src.gui.SpriteSheet.<init>(SpriteSheet.java:17)
at com.brickbattle.client.src.gui.SpriteSheet.<clinit>(SpriteSheet.java:8)
再次感谢!
推荐答案
这个问题基本上和ImageIO无关,而是Class
/ClassLoader.getResource
或者getResourceAsStream
有效.
This problem is basically unrelated to ImageIO, but rather how Class
/ClassLoader.getResource
or getResourceAsStream
works.
有关解释,请参阅此答案.
无论如何,这些获取资源的方式只能从类路径中读取(即user.dir
在这里永远不会有帮助).
In any case, these ways of obtaining a resource will only be able to read from classpath (ie. user.dir
will never help here).
这应该有效:
ImageIO.read(getClass().getResource("/path/to/resource"));
其中路径是相对于类路径的根(由前导/指定).
Where the path is relative to the root of the classpath (specified by the leading /).
如果您的资源不在类路径上,只需使用:
If your resources are not on the classpath, simply use:
ImageIO.read(new File("path/to/resource");
路径相对于启动应用程序的目录.
这篇关于尝试使用 ImageIO.read(class.getResource(URL)) 加载图像但 getResource 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!