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 - 百度坐标,返回目标附近有哪些坐标2. html5 - 只用CSS如何实现input框的宽度随框里输入的内容长短自动适应?3. javascript - 关于jsp中嵌入js代码的问题4. PHP类中的$this5. html - 急求,能否用flex或者高端点的CSS来布局这个图呢?不想用浮动或定位了6. java - 微信开发遇到一个 bug。。access_token 有关的7. Python操作sqlserver是异步的吗?8. python 正则表达式提取9. python - 这句是什么错误?10. angular.js - angularjs 百度统计怎么统计

网公网安备