带有 ImageIO 的 Java 未知来源 [英] Java unknown source with ImageIO

查看:44
本文介绍了带有 ImageIO 的 Java 未知来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序在 Netbeans 中运行良好,但在浏览器中运行小程序时出现以下错误:

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)

我打开了 jar 以验证 png 文件是否正确放置在那里.我不确定为什么以下不起作用(当然在 try 块中):

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());

感谢您的建议.

推荐答案

问题出在您的路径字符串中.改用这个:

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

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

(注意路径前的/)

这篇关于带有 ImageIO 的 Java 未知来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆