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

This is a question that was asked 100 times on this site, but I looked at all of them, and although all of them were resolved, none of the solutions worked for me.

This is 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 you run the code, the following error occurs:

 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 the child class and is the name of the image (for example, image.png). I also made sure that my resources folder is in the root of the project folder and is included as the source folder in the eclipse project. I also made sure that System.getProperty("user.dir") points to the correct location. I also tried using getResource () instead of getResourceAsStream (), but it still does not work.

+8
java bufferedimage javax.imageio
source share
7 answers

Try using: -

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

where the res folder is at the same level as your src folder. In addition, if you notice, the slash / in front of the res folder name has been deleted.

+10
source share

The path passed as the argument to getResourceAsStream () must belong to the pathpath set. So try changing this

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

to

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

Try the following:

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

You can try the following:

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

Try using the following

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

I had the same problem. At first I used the path "my_image.png", but it did not work, so I searched everywhere and tried other solutions hosted on this site, but none of them worked. I solved mine by changing the code from this

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

to that

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

Hope this helps, although this question was posted 5 years ago.

0
source share

try it

 private BufferedImage get(String path) throws IOException{ URL url = this.getClass().getClassLoader().getResource(path); String thing = url.getFile(); return ImageIO.read(new File(thing)); } 
-2
source share

All Articles