使用 Eclipse IDE。线路:
getClass().getResource("/res/bitmaps/image.png");
返回 null
。我在项目的根目录中创建了 res
文件夹。
感兴趣的代码是:
bImage = ImageIO.read(getClass().getResource("/res/bitmaps/image.png"));
并抛出异常:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1378)
at com.example.game.resource.Resources._loadImage(Resources.java:31)
at com.example.game.GameComponent.<init>(GameComponent.java:19)
at com.example.game.GameFrame.<init>(GameFrame.java:8)
at com.example.game.GameFrame.main(GameFrame.java:13)
有什么帮助吗?
最佳答案
ImageIO.read(getClass().getResourceAsStream("res/drawable/image.png"));
确保 res 文件夹位于类路径中,使用项目属性 > Java 构建路径 > 源选项卡进行验证。如果不在类路径中,可以通过右侧的添加文件夹..按钮添加。
关于java - getResources() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9867273/