为什么我的BufferedImage从ImageIO.read()接收空值 [英] Why is my BufferedImage receiving a null value from ImageIO.read()

查看:733
本文介绍了为什么我的BufferedImage从ImageIO.read()接收空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"));

首先,是的,我确实将图像文件夹添加到了我的类路径中。

First of all, yes I did add the image folder to my classpath.

为此,我收到错误 java.lang.IllegalArgumentException:input == null!

我不明白为什么上面的代码不起作用。从我阅读的所有内容中,我看不出为什么不这样做。有人告诉我应该使用 FileInputStream 而不是 GetResourceAsStream ,但是,正如我刚才所说,我不会不明白为什么。我已经阅读了有关方法和各种指南的文档,这似乎可以正常工作。

I don't understand why the above code doesn't work. From everything I read, I don't see why it wouldn't. I've been told I should be using FileInputStream instead of GetResourceAsStream, but, as I just said, I don't see why. I've read documentation on the methods and various guides and this seems like it would work.

编辑:好的,尝试清除一些有关我所拥有的东西在类路径中。

Okay, trying to clear some things up with regards to what I have in the classpath.

这是在Eclipse中创建的项目。一切都在项目文件夹 DreamGame 中,包括 Images文件夹。 DreamGame 当然在类路径中。我知道这行得通,因为我正在读取 / Images 中的文本文件,其中包含有关gif信息的代码。

This is a project created in Eclipse. Everything is in the project folder DreamGame, including the "Images" folder. DreamGame is, of course, in the classpath. I know this works because I'm reading a text file in /Images with info on the gif earlier on in the code.

所以我有: /DreamGame/Images/player.gif

编辑2:当前在原始帖子中正在传递的所有内容;否 /DreamGame/Images/player.gif ,仅 /Images/player.gif 。这是从 ImagesLoader 类中的方法创建的,该方法在创建 PlayerSprite 中的对象时调用。主要类是 DreamGame 。我正在使用 Run 选项直接从Eclipse运行代码,没有特殊参数

Edit 2: The line that's currently in the original post is all that's being passed; no /DreamGame/Images/player.gif, just /Images/player.gif. This is from a method in the class ImagesLoader which is called when an object from PlayerSprite is created. The main class is DreamGame. I'm running the code right from Eclipse using the Run option with no special parameters

试图弄清楚如何查找哪个类加载器正在加载该类。抱歉,与大多数人相比,我是这个新手。

Trying to figure out how to find which class loader is loading the class. Sorry, compared to most people I'm pretty new at this.

好的,这就是 getClassLoader()让我知道: sun.misc.Launcher$AppClassLoader@4ba778

Okay, this is what getClassLoader() gets me: sun.misc.Launcher$AppClassLoader@4ba778

getClass()。 getResource(getClass()。getName()+ .class)返回 /home/gixugif/Documents/projects/DreamGame/bin/ImagesLoader.class

图像文件也被放在 bin 中。为了再次检查,我从 bin 中删除​​了文件,清理了项目,然后运行它。仍然存在相同的问题,并且图像文件返回到 bin

The image file is being put in bin as well. To double check I deleted the file from bin, cleaned the project, and ran it. Still having the same problem, and the image file is back in bin

推荐答案

基本上, Class.getResourceAsStream 并没有按照您的想法做。

Basically, Class.getResourceAsStream doesn't do what you think it does.

它试图获取一个相对于该类的类加载器的资源-因此,除非您有一个文件系统根目录为根的类加载器,否则找不到您要查找的文件。

It tries to get a resource relative to that class's classloader - so unless you have a classloader with your filesystem root directory as its root, that won't find the file you're after.

听起来您应该真的应该有以下内容:

It sounds like you should quite possibly really have something like:

BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"))

(编辑:显示的原始代码是不同的,并且具有完整的文件系统路径。)

( The original code shown was different, and had a full file system path.)

,然后确保将图像复制到类加载器的适当位置当前类中的一个以获取 Images 目录。当您将其打包到jar文件中时,您也希望在其中包含 Images 目录。

and you make sure that the images are copied into an appropriate place for the classloader of the current class to pick up the Images directory. When you package it into a jar file, you'd want the Images directory in there too.

编辑:这可能是问题所在:

This bit may be the problem:


首先,是的,我确实将图像文件夹添加到了我的类路径中。

First of all, yes I did add the image folder to my classpath.

images文件夹不应该在类路径中-Images文件夹的 parent 应该在类路径中,这样当类加载器出现时查找一个 Images 目录,它将在其根目录下找到它。

The images folder shouldn't be in the classpath - the parent of the Images folder should be, so that then when the classloader looks for an Images directory, it will find it under its root.

这篇关于为什么我的BufferedImage从ImageIO.read()接收空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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