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文件夹中,因此我认为原始图片是正确的。
相关文章:
1. javascript - 手机点击input时,button会被顶上去?求解决!!!2. HTML5禁止img预览该怎么解决?3. dologin说是没有定义4. javascript - 关于js原生事件的绑定与解除绑定5. mysql - 数据库建字段,默认值空和empty string有什么区别 1106. angular.js - 在ionic下,利用javascript导入百度地图,pc端可以显示,移动端无法显示7. 代码如下,java用BigDecimal四舍五入怎么还有问题呀?哪种方式是完全没有问题的?8. python3 脚本调用shell 指令如何获得返回值9. mybatis - Java关于Mysql的随机id生成10. javascript - 函数声明和匿名函数有什么不同?(前端小白求助。。)