0

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:

enter image description here

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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
Andrii H.
  • 1,682
  • 3
  • 20
  • 40
  • Why do you thing that `xTheme.class.getResource()` should find the resources under `res/`? What makes you think that could possibly work? – JB Nizet May 27 '17 at 12:45
  • Well, I also tried to add res to the path, but that doesn't work.. – Andrii H. May 27 '17 at 12:47
  • res/images, /res/images, ./res/images. None of this works. – Andrii H. May 27 '17 at 12:48
  • 1
    You're not answering my question. You're using a method named getResource(), on the type java.lang.Class. Why don't you read its javadoc to know what it does and how it works, instead of trying random things? Hint: it delegates to the ClassLoader to load resources. The ClassLoader doesn't load resources from arbitrary locations, magically. It finds resources from jars and directories that are in the runtime classpath. – JB Nizet May 27 '17 at 12:50

2 Answers2

1

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.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

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.

screenshot

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.
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49