tyler
tyler

Reputation: 589

java.lang.IllegalArgumentException: input == null! when using ImageIO.read to load image as bufferedImage

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.

Upvotes: 15

Views: 57181

Answers (12)

off oof
off oof

Reputation: 68

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.

Upvotes: 0

Sudo Nim
Sudo Nim

Reputation: 31

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.

Upvotes: 3

Shawn Trzandel
Shawn Trzandel

Reputation: 21

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.

Upvotes: 2

praneeth1189
praneeth1189

Reputation: 11

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);
     }
   }
 }

Upvotes: 0

Carlson
Carlson

Reputation: 11

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.

Upvotes: 1

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.

Upvotes: 0

user1441664
user1441664

Reputation: 11

Try using the following

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

Upvotes: 0

Fito
Fito

Reputation: 1

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));      
}

Upvotes: -3

1218985
1218985

Reputation: 8032

You can try this:

image = ImageIO.read(getClass().getResource("/resources/" + imgName));

Upvotes: -1

PSR
PSR

Reputation: 40358

Try this:

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

Upvotes: 0

Kishore
Kishore

Reputation: 839

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));

Upvotes: 1

Rahul
Rahul

Reputation: 45090

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.

Upvotes: 14

Related Questions