输入== null java错误

时间:2013-07-04 19:43:35

标签: java image exception io null

嗨我已经得到一个恼人的错误不断它给我输入== null错误你得到一个图像不能加载。这是代码。

  package Graphics;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class spritesheet {
    public String path;
    public int width;
    public int height;
    public int[] pixels;


        public spritesheet(String path){
            BufferedImage image = null;

            try {
                image =
                        ImageIO.read(spritesheet.class.getResourceAsStream(path));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(image == null){
                return;
            }
            this.path = path;
            this.width = image.getWidth();
            this.height = image.getHeight();

            pixels = image.getRGB(0, 0, width, height, null ,0  , width);
        }           
        public int[][] getsprite(int x, int y, int[][] pixels){
            int i = 0;
            for(int xx = 0;xx< 7;x++){
                for(int yy = 0; yy< 7;yy++){
                    pixels[xx][yy] = this.pixels[i];
                    i++;
                }
            }
            return pixels;
        }
    }   

我已经检查过,目录完全正确。

错误

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at Graphics.spritesheet.<init>(spritesheet.java:20)
    at Tile.Tile.<init>(Tile.java:9)
    at Tiles.Wall.<init>(Wall.java:8)
    at Tiles.tiles.<init>(tiles.java:6)
    at Core.core.<init>(core.java:27)
    at Core.core.main(core.java:140)

1 个答案:

答案 0 :(得分:1)

使用FileImageInputStream阅读您的文件

ImageIO.read(new FileImageInputStream(new File(path)));

因为,使用绝对路径(C:\ test.png)不使用getresourceAsStream()用于从类路径。其次,你不需要在你的路径中逃避正斜杠。使用C:/testfile.pngC:\\testfile.png

参考:
ImageIO#read(javax.imageio.stream.ImageInputStream)