Java unknown source with ImageIO

流过昼夜 提交于 2019-12-11 09:36:01

问题


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

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