15

This is a question that has been asked like 100 times on this site, but I have looked at all of them and even though they all were solved, none of the solutions worked for me.

Here's what my code looks like:

public Button1(Client client, String imgName) {
    this.client = client;   

    try {
        this.icon = ImageIO.read(this.getClass().getResourceAsStream("/resources/" + imgName));
    } catch (IOException e) {
        e.printStackTrace();
    }

When the code runs it results in the following error:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)

The string imgName is passed to the constructor from a child class and is the name of an image (e.g. image.png). I also have made sure that my resources folder is in the root of the project folder, and is included as a source folder in the eclipse project. I've also made sure that System.getProperty("user.dir") points to the correct location. I have also tried using getResource() instead of getResourceAsStream(), but it still does not work.

tyler
  • 589
  • 2
  • 5
  • 9
  • 6
    Typically, if the `resources` folder is actually marked as a source folder in Eclipse, you shouldn't be including the `/resources` portion in the File location above. Instead, the `resources` folder would be the "root" and you should just be able to do `"/" + imgName`. – Peter Mularien Sep 20 '13 at 18:48
  • Just putting my personal experience out there. I was investigating a similar issue for an hour, when I noticed my .jpg in resources started with a capital letter, while I had defined path to .jpg, starting with lowercase. So captain obvious alert, but still - beware the file names :) Good luck. – Alex Apr 07 '16 at 01:47
  • [This answer](http://stackoverflow.com/a/17753326/731314) was the solution when I got this same error. – Gary Jun 10 '16 at 23:55

12 Answers12

13

Try using this:-

this.icon = ImageIO.read(new FileInputStream("res/test.txt"));

where res folder is present at the same level as your src folder. Also, if you notice, the slash / before the res folder name was removed.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Just tried this - it didn't throw an error, but the images did not appear. – tyler Mar 15 '13 at 04:59
  • check if your image file is null or not. – Rahul Mar 15 '13 at 05:03
  • @Joza100 - I recommend you read the javadocs which should give you a better picture of what is happening and why is this working. – Rahul May 19 '20 at 05:28
  • 1
    @SudoRahul the funny thing is that using Class.getClass().getResource() worked for me before, but when i tried something else recently, that same thing didn't work and i had to switch ti to this. That's why im asking. Alsmot as if something changed. – Joza100 May 19 '20 at 10:19
3

I solved mine by changing my code from this

 image = ImageIO.read(SpriteSheet.class.getResourceAsStream("res/image.png"));

to this

 image = ImageIO.read(SpriteSheet.class.getResourceAsStream("/image.png"));

Hope this helps.

Sudo Nim
  • 31
  • 3
2

I know this is pretty old, but I just had the same issue.

Check to make sure that your image extensions aren't capital.

In my resources folder for images I had "enemy.PNG", but I was trying to load "enemy.png" which you would think would work but doesn't.

so, just make your extensions aren't capitalized.

1

The path passed as the argument to getResourceAsStream() should be relative to the classpath set. So try changing this

this.icon = ImageIO.read(this.getClass().getResourceAsStream("/resources/" + imgName));

to

this.icon = ImageIO.read(this.getClass().getResourceAsStream("resources/" + imgName));
Kishore
  • 819
  • 9
  • 20
  • All of the other threads with this problem say that the slash should be there actually, but neither work for me anyway. – tyler Mar 15 '13 at 04:58
  • 1
    try checking for imgName for null. – Kishore Mar 15 '13 at 05:02
  • I've checked everything for null with print statements already. Nothing comes back as null or anything unexpected except for the `this.getClass().getResourceAsStream("/resources/" + imgName)` statement. – tyler Mar 15 '13 at 05:04
  • Check the classpath of your project.It should have the resources folder. If not add it to your classpath and use the relative path. – Kishore Mar 15 '13 at 05:18
  • The resource folder is and has always been in my classpath. This is really frustrating... – tyler Mar 15 '13 at 05:39
1

This may come as a "No, Duh!" to many on this site, but it is always important to point out how literal Java is. Case sensitivity is key, especially if you .jar a file.

If your program works fine with compiling and then running but suddenly is getting this issue when you .jar your files. Make sure to check you Case on your folders / files and your references in your code. (As well as make sure they are in your .jar)

Hope this helps anyone that ends up here looking at the same issue.

Carlson
  • 11
  • 1
0

Try this:

this.icon = ImageIO.read(this.getClass().getResource("/resources/" + imgName));
Javier
  • 12,100
  • 5
  • 46
  • 57
PSR
  • 39,804
  • 41
  • 111
  • 151
0

Try using the following

this.icon = ImageIO.read(this.getClass().getResourceAsStream("../resources/" + imgName));
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
0

Is the resource folder a class folder in eclipse? Right click on the project -> Properties -> Java Build Path -> Libraries -> Add Class Folder... -> (select the res folder) and add it as a class folder.

0

I was facing this error due to a bug in my code. I was trying to extract (conn.getInputStream()) from a different connection object than what it should have been. I fixed the connection object variable and it started working.

BufferedImage image;
 try (InputStream in = new BufferedInputStream(conn.getInputStream())) {
   image = ImageIO.read(in);
   File file = new File(fullImageFilePath);
   synchronized (file.getCanonicalPath().intern()) {
     if (!file.exists()) {
         ImageIO.write(image, "png", file);
     }
   }
 }
0

Instead of this.icon = ImageIO.read(this.getClass().getResourceAsStream("/resources/" + imgName));

Use this.icon = ImageIO.read(new File("Full Path");

I do not know why the first snippet does not work, but new File() has always worked for me.

off oof
  • 43
  • 5
-1

You can try this:

image = ImageIO.read(getClass().getResource("/resources/" + imgName));
1218985
  • 7,531
  • 2
  • 25
  • 31
-3

Try This

private BufferedImage get(String path) throws IOException{    
    URL url = this.getClass().getClassLoader().getResource(path);     
    String thing = url.getFile();       
    return ImageIO.read(new File(thing));      
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Fito
  • 1
  • 2
    Please correct the code of the question, not copy/paste a not related code without contexts or explanations... – Kartoch Sep 20 '13 at 18:55