0

I have the code below and I am trying to catch an exception and print out something if the file == null so it doesn't throw an exception but I am having problems solving this.

public class Controller {
    private ImageWindow IW = new ImageWindow(this);
    private Model M = new Model(this.IW);
    private File file = null;

    public void openImage() {
        File file = IW.ChooseImageFile();
        if (file != null) {
            M.loadImage(file);
        }
        this.IW.setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        SwingUtilities.invokeLater(() -> new Controller());
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

-1

Usually I would use a try/catch statement for exception management in Java. Reference

try {
    Enter code here
} catch (NullPointerException npe) {
    System.out.println(“Error Message”);
}
  • In normal code, you shouldn't catch a `NullPointerException`, it almost always indicates a bug (an unchecked dereference of null). – Mark Rotteveel Nov 20 '20 at 13:43