问题
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.
回答1:
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)
回答2:
For the record and If someone is stucked with this type of error for some reason eclipse auto changed the build path and blocked the image path from being packaged.
To change this setting just right click on the project name
-> Properties
-> Java Build Path
-> Source Tab
and check that the folder containing the images is not excluded, if it is just remove that rule.
来源:https://stackoverflow.com/questions/12841741/java-unknown-source-with-imageio