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 - .vue页面跳转问题2. java - 微信小程序中 无缘无故的提示(图片的加载失败)3. [python2]local variable referenced before assignment问题4. python - TypeError: tryMsgcode() takes exactly 2 arguments (0 given)5. javascript - 像这种内联Js绑定方式,如何定位到js代码?6. python - 一个程序中的切片问题7. javascript - 使用form进行页面跳转,但是很慢,如何加一个Loading?8. javascript - vue-cli中 用proxyTable实现了跨域,用get访问没有问题,但通过 post传数据就出现了问题9. css - 手机app中rem的基准值计算错误10. module - python模块from import语句相对导入,加不加点号有什么区别?

网公网安备