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. 主从备份 - 跪求mysql 高可用主从方案2. python - django 里自定义的 login 方法,如何使用 login_required()3. python如何不改动文件的情况下修改文件的 修改日期4. android-studio - Android 动态壁纸LayoutParams问题5. javascript - git clone 下来的项目 想在本地运行 npm run install 报错6. angular.js - 不适用其他构建工具,怎么搭建angular1项目7. mysql主从 - 请教下mysql 主动-被动模式的双主配置 和 主从配置在应用上有什么区别?8. mysql优化 - mysql count(id)查询速度如何优化?9. node.js - 使用 superagent 抓取 UTF-8网站乱码10. sql语句如何按or排序取出记录
