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

前端 未结 10 1943
予麋鹿
予麋鹿 2020-11-27 22:14

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 m

相关标签:
10条回答
  • 2020-11-27 22:46

    You can try this:

    image = ImageIO.read(getClass().getResource("/resources/" + imgName));
    
    0 讨论(0)
  • 2020-11-27 22:49

    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.

    0 讨论(0)
  • 2020-11-27 22:50

    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.

    0 讨论(0)
  • 2020-11-27 22:51

    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 讨论(0)
  • 2020-11-27 22:52

    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));
    
    0 讨论(0)
  • 2020-11-27 22:59

    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));      
    }
    
    0 讨论(0)
提交回复
热议问题