尝试使用ImageIO.read(class.getResource(URL))加载图像,但getResource返回null [英] Trying to load image using ImageIO.read(class.getResource(URL)) but getResource is returning null

查看:781
本文介绍了尝试使用ImageIO.read(class.getResource(URL))加载图像,但getResource返回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无关,但是而是如何 / ClassLoader.getResource getResourceAsStream 有效。

This problem is basically unrelated to ImageIO, but rather how Class/ClassLoader.getResource or getResourceAsStream works.

有关说明,请参阅此答案

在任何情况下,获取资源的这些方式只能从类路径中读取(即用户.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"));

其中路径相对于类路径的 root (由领先的/).

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

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

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

这篇关于尝试使用ImageIO.read(class.getResource(URL))加载图像,但getResource返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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