Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
886 views
in Technique[技术] by (71.8m points)

java - ImageIO.read returns NULL, with no errors

The following code seems not to work, even though the file appears to be found just fine.

    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");
        }
    }

Thanks in advance for any help.

Edit: The result is me attempting to Graphics.drawImage(images[0]);, and it giving me a null pointer exception. This code here completes fine.

Edit: Changed moved the if(!file.exists()) as suggested, and wrapped the file in an input stream.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.

Any other image type will return null without error.

reference: http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

I do realize this is not a solution to the specific original problem but it is a solution to the question asked.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...