ImageIO无法读取我的文件? (输入== NULL)

时间:2013-11-16 14:20:52

标签: java image javax.imageio

当我尝试从res文件夹中读取图像时,出现此错误:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at SpritePractice.render(SpritePractice.java:103)
at SpritePractice.run(SpritePractice.java:71)
at SpritePractice.start(SpritePractice.java:123)
at SpritePractice.main(SpritePractice.java:131)

图像本身(MarioSpriteSheet.png)位于res文件夹中,我拼写正确的名称。这有什么问题?

BufferedImage spriteSheet = new BufferedImage(WIDTH*2,HEIGHT*2, BufferedImage.TYPE_INT_RGB);
    try
    {
    spriteSheet = ImageIO.read(getClass().getResourceAsStream("/res/MarioSpriteSheet.png"));
    }catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        pixelsFromImage = ((DataBufferInt) spriteSheet.getRaster().getDataBuffer()).getData();
    }

1 个答案:

答案 0 :(得分:2)

从文件名中省略/res/。如果CLASSPATH上有res,则不需要它。