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

我与影子孤独终老i 提交于 2019-11-26 23:11:06

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.

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

I had the exact same problem. I used the path "my_image.png" at first but it did not work, so I searched everywhere and tried the other solutions posted on this site but none of them worked. 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"));

I hope this helps, even though this question was posted 5 years ago.

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.

PSR

Try this:

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

You can try this:

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

Try using the following

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

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.

Fito

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));      
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!