[英]ImageIO.read getResource error
I am experiencing a strange problem. 我遇到一个奇怪的问题。 Here is my snippet of code: 这是我的代码片段:
...
public xProgressBar(xTheme theme) {
try {
this.update = ImageIO.read(xTheme.class.getResource("/images/" + xThemeSettings.PROGRESSBAR_IMAGES[0]));
}
...
And when I run a program, I am getting the following error: 当我运行程序时,出现以下错误:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
Here is a file structure: 这是一个文件结构:
As you can see, the res
folder is at the root with the src
folder. 如您所见, res
文件夹位于src
文件夹的根目录。 I have read a lot of similar questions, but nothing helped. 我读过很多类似的问题,但没有帮助。
In order for getResource
to find a file, the corresponding folder ( res
in this case) needs to be in the classpath
. 为了使getResource
查找文件,相应的文件夹(在这种情况下为res
)必须位于classpath
。 If it's not in the classpath, InputStream
returned by getResource
will always be null
. 如果不在类路径中,则getResource
返回的InputStream
始终为null
。
Here's how to add folder(s) into classpath
. 这是将文件夹添加到classpath
。
Your call .getResource("/images/...")
didn't succeed and it returned null
. 您的呼叫.getResource("/images/...")
未成功,返回null
。 Hence you were calling ImageIO.read(null)
and got a IllegalArgumentException
. 因此,您正在调用ImageIO.read(null)
并获得了IllegalArgumentException
。
For your resources located in the res
folder to be found by ...getResource(...)
, you need to make res
a source folder of your Eclipse project. 为了使资源位于...getResource(...)
可以找到的res
文件夹中,您需要将res
设置为Eclipse项目的源文件夹。 To achieve this: Right-click on your res
folder, in the popup-menu select Build path
-> Use as Source Folder
. 为此,请执行以下操作:右键单击res
文件夹,在弹出菜单中选择Build path
> Use as Source Folder
。
You will notice then 你会注意到
res
will appear with the same icon as your src
folder. res
将显示与src
文件夹相同的图标。 res
will be added to the .classpath
file of your project. res
将添加到项目的.classpath
文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.