giddian
giddian

Reputation: 3

How to test if BufferedImage using ImageIO picks up a 'null'

I am trying to set my program (java using netbeans) to do one thing if it finds the image its looking for, and another thing if it doesn't...

this is what I have got so far however it never completes the else statement. I believe this is because "image" is not technically null, as it still has corresponds to the filename entered, however I am not sure how to set java to do something based on if the filename is not found within the directory.

public void displayImage(String strfilename, JLabel JLlabel) {

    try {
        JLabel label = JLlabel;

        String FileName = strfilename;
        BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
        if(image!=null){
        ImageIcon icon = new ImageIcon(image);

        label.setIcon(icon);}
        else{
        BufferedImage image2 = ImageIO.read(new File("NOIMAGE.jpg"));
        ImageIcon icon2 = new ImageIcon(image2);
        label.setIcon(icon2);
        }
    } catch (IOException ioe) {
    }
}

If anyone could help me with this I would be very grateful

Upvotes: 0

Views: 2216

Answers (2)

Touniouk
Touniouk

Reputation: 375

You're swallowing the exception. You don't need an if{}else{} loop since you already have a try{}catch{}.

String FileName = file;
try {
    BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
    // Code for when the image is found
} catch (IOException ex) {
    // Code for when the image is not found
}

EDIT: As @haraldK pointed out, you can have a file that exists but is unreadable, in which case a NullPointerException will be thrown.

You can handle them both in the catch clause.

public void displayImage(String strfilename, JLabel label) {
    try {
        BufferedImage image = ImageIO.read(new File(strfilename + ".jpg"));
        ImageIcon icon = new ImageIcon(image); // Can throw NullPointerException if the file is found but is unreadable
        label.setIcon(icon);

    } catch (IOException | NullPointerException ex) {
        ImageIcon icon = new ImageIcon("NOIMAGE.jpg");
        label.setIcon(icon);
    }
    // You're probably going to have to pack or validate your container here
}

One thing worth noting is that this does not check exceptions for the NOIMAGE, you might want to add that.

This is better than just calling File.exists() as it also handles files that exist but are unreadable (text file and whatnot).

Upvotes: 2

Basil Battikhi
Basil Battikhi

Reputation: 2668

you can check the file if exist before you process this method also i removed some unnecessary code.

public void displayImage(String strfilename, JLabel JLlabel)  {
// declare only one reference you don't need two references 

BufferedImage image=null;

if(!isImageExist(strfilename)){
// assign the NOIMAGE if image not found
 image = ImageIO.read(new File("NOIMAGE.jpg"));    
} else {

    try {
        // assign the image if found
        image = ImageIO.read(new File(strfilename + ".jpg"));

    } catch (IOException ioe) {
      ioe.printStackTrace();
   }
}
     ImageIcon icon = new ImageIcon(image);
    //setting the image once instead of repeating the code in if and else blocks also you don't need to add another reference to JLlabel because you are already got it from a parameter 
     JLlabel.setIcon(icon);
}
private boolean isImageExist(String imageName) {
return new File(imageName.jpg).exist();
}

Upvotes: 1

Related Questions