简体   繁体   中英

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. 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 . If it's not in the classpath, InputStream returned by getResource will always be null .

Here's how to add folder(s) into classpath .

Your call .getResource("/images/...") didn't succeed and it returned null . Hence you were calling ImageIO.read(null) and got a 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. To achieve this: Right-click on your res folder, in the popup-menu select Build path -> Use as Source Folder .

屏幕截图

You will notice then

  • res will appear with the same icon as your src folder.
  • res will be added to the .classpath file of your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM