ImageIO.read返回NULL,没有错误
以下代码似乎不起作用,即使该文件看起来很好也是如此。
    images = new BufferedImage[32];    FileInputStream fis = null;
    for (int i = 0; i < 32; i++) {
        File file = new File("tiles\\"+i+".bmp");
        if (!file.exists()){
            System.out.println("File  "+i+" failed");
        }
        try { 
            fis = new FileInputStream(file); 
        } catch (FileNotFoundException e) { 
            System.err.println(e + "" + i); 
        }
        try { 
            images[i] = ImageIO.read(fis); 
        } catch (IOException e) { 
            System.err.println(e + "" + i); 
        }
        if (images[i] == null) {
            System.out.println("Image "+i+" failed");
        }
    }
在此先感谢您的帮助。
编辑:结果是我试图去Graphics.drawImage(images [0]);,它给了我一个空指针异常。这段代码可以很好地完成。
编辑:更改按建议移动if(!file.exists()),并将文件包装在输入流中。
回答:
如果找不到注册的 将返回null 。请检查您是否已注册任何
。
我认为此代码段可以帮助您
File file = new File("bear.jpg"); // I have bear.jpg in my working directory      FileInputStream fis = new FileInputStream(file);  
    BufferedImage image = ImageIO.read(fis); //reading the image file
您只需要将文件包装到 ,然后将其传递给
以上是 ImageIO.read返回NULL,没有错误 的全部内容, 来源链接: utcz.com/qa/425155.html

