ImageIO wont import BufferedImage correctly

允我心安 提交于 2019-12-11 18:08:51

问题


Error thrown--

Exception in thread "Thread-3" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source)

Code--

try {
        playerImage = ImageIO.read(Player.class
                .getResourceAsStream("/toon.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

File path--

H:\workspace\Isaac\resources\toon.png

Ive researched this a bit but none of the solutions have really worked for me. any ideas?


回答1:


If Isaac is your project folder, then the way your are reading should work. Please check whether the directory resources is a source folder (in eclipse it should have an icon of a folder with an overlay of a java package), if not then right click on it and got to Build path and choose Use as Source Folder.

Your directory structure should look something like this:

before compiling

H:\workspace\Isaac
├───resources
│       toon.png
│
└───src
        Player.java

after compiling

H:\workspace\Isaac
├───bin
│       Player.class
│       toon.png
│
├───resources
│       toon.png
│
└───src
        Player.java

getResourceAsStream looks for the image in bin and not in the one in resources



来源:https://stackoverflow.com/questions/27215235/imageio-wont-import-bufferedimage-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!