java 尝试使用 ImageIO.read(class.getResource(URL)) 加载图像但 getResource 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17002906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:40:04  来源:igfitidea点击:

Trying to load image using ImageIO.read(class.getResource(URL)) but getResource is returning null

javabufferedimagejavax.imageiogetresource

提问by user2466959

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 nullfor some reason.

我一直在和我的伙伴一起制作 2D 游戏,并且通过一些 Youtube 教程学习了很多关于一些基本游戏开发概念的知识。我正在学习的一件事是精灵(对于那些不知道的人,要渲染到屏幕上的 2D 图像)以及如何在我的游戏中使用它们。我一直在使用,ImageIO.read(this.class.getResource(pathToMySprite))但似乎由于某种原因getResource()正在返回null

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.

我一直在修改路径,在它前面添加“/”,删除“/”,放置 user.dir 属性以查看它是否需要整个路径,但我仍然遇到相同的错误.

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.

任何和所有的帮助表示赞赏。我没有评论代码(当我到达可以坐下来对我完成的东西感到满意的地方时,我通常会这样做)但这是一个非常小的课程,所以我想你们将能够理解什么是一切顺利。

The folder that holds the image ISin the class path of my project. I've also included the error:

保存影像的文件夹IS在我的项目的类路径。我还包括错误:

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)

Thanks again!

再次感谢!

回答by haraldK

This problem is basically unrelated to ImageIO, but rather how Class/ClassLoader.getResourceor getResourceAsStreamworks.

这个问题基本上与 ImageIO 无关,而是与Class/ClassLoader.getResource或如何getResourceAsStream工作有关。

For an explanation, see this answer.

有关解释,请参阅此答案

In any case, these ways of obtaining a resource will only be able to read from classpath(ie. user.dirwill never help here).

在任何情况下,这些获取资源的方式都只能从类路径中读取(即user.dir在这里永远不会有帮助)。

This should work:

这应该有效:

ImageIO.read(getClass().getResource("/path/to/resource"));

Where the path is relative to the rootof the classpath (specified by the leading /).

其中路径相对于类路径的(由前导 / 指定)。

If your resources are not on the classpath, simply use:

如果您的资源不在类路径上,只需使用:

ImageIO.read(new File("path/to/resource");

Where the path is relative to the directory your application was launched from.

路径相对于您的应用程序启动的目录的位置。