Search code examples
javapngembedded-resourcejavax.imageio

Java unknown source with ImageIO


My program runs fine in Netbeans, but I get the following error when I run my applet in a browser:

java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)

I opened the jar to verify that the png files are correctly placed there. I'm not certain why the following doesn't work (in a try block, of course):

BufferedImage beam = ImageIO.read(this.getClass().getResource("images/beam.png"));

I've tried other things like the following, but suspect the problem might be something else.

URL url = this.getClass().getResource("images/beam.png");
BufferedImage beam = ImageIO.read(url.openStream());

Your advice is appreciated.


Solution

  • The problem is in your path String. Use this instead:

    BufferedImage beam = ImageIO.read(this.getClass().getResource("/images/beam.png"));
    

    (Note the / before the path)