java.lang.IllegalArgumentException: input == null! Solution

Translation Notice
This article was machine-translated using DeepSeek-R1.

  • Original Version: Authored in Chinese by myself
  • Accuracy Advisory: Potential discrepancies may exist between translations
  • Precedence: The Chinese text shall prevail in case of ambiguity
  • Feedback: Technical suggestions regarding translation quality are welcomed

Recently encountered an issue while developing a Java game when handling images:

Complete Error

1
2
3
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1356)
    at ImageIOTest.main.<init>(main.java:23)

Code Snippet

1
BufferedImage img = ImageIO.read(getClass().getResourceAsStream(FILENAME));

Solution

Searched online and found many answers saying it’s incorrect image path. But I’ve checked the image path $555n$ times and found no issues:

So? Dead end?
Actually, modifying the code like this fixes it:

1
BufferedImage img = ImageIO.read(new File(FILENAME));
Built with Hugo
Theme Stack designed by Jimmy