Java ImageIO.read(getClass()。getResource())返回null
假设您的课程在package中view.random.name,则
getClass().getResource('gate_and.png')
会在以下位置寻找资源
/view/random/name/gate_and.png
相对于类路径的根。您显然那里没有该名称的资源。
通过设置project/images为构建路径条目,Eclipse将在类路径中包括所有内容。因此,您的资源将显示在
/gate_and.png
您可以使用
getClass().getResource('/gate_and.png')
请注意 开头/,这意味着 开始查看类路径的根 ,即。这是一条绝对的道路。
所有这些规则在javadoc中进行了解释。
解决方法线
andImg = ImageIO.read(getClass().getResource('gate_and.png'));
失败于
Exception in thread 'AWT-EventQueue-0' java.lang.IllegalArgumentException: input == null!
我正在使用Eclipse,并且在bin文件夹下的导航视图中有文件gate_and.png,表明该文件位于构建路径中。
在包浏览器视图中,我有
project/src/view/class - This is the class that has the code above.
和
project/images/gate_and.png
我右键单击项目文件夹>构建路径>链接源以将images文件夹添加为源,再次执行此操作会提供一条确认消息,指出源中已存在图像。
我还尝试过将gate_and.png更改为images /gate_and.png和/images/gate_and.png,但是由于图像gate_and.png在bin文件夹中,因此我认为原始图片是正确的。
相关文章:
