Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 452
tyler Avatar asked Mar 15 '13 04:03

tyler


People also ask

Can ImageIO read JPG?

Just use the read method of the Java ImageIO class, and you can open/read images in a variety of formats (GIF, JPG, PNG) in basically one line of Java code.

What is Java Lang IllegalArgumentException?

The IllegalArgumentException is an unchecked exception in Java that is thrown to indicate an illegal or unsuitable argument passed to a method. It is one of the most common exceptions that occur in Java.

What is ImageIO in Java?

ImageIO. A class containing static convenience methods for locating ImageReader s and ImageWriter s, and performing simple encoding and decoding. ImageReader. An abstract superclass for parsing and decoding of images.

What does ImageIO read do?

Image I/O recognises the contents of the file as a JPEG format image, and decodes it into a BufferedImage which can be directly used by Java 2D.


2 Answers

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.

like image 111
Rahul Avatar answered Sep 28 '22 04:09

Rahul


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.

like image 36
Shawn Trzandel Avatar answered Sep 28 '22 03:09

Shawn Trzandel