When I need to close a Scanner by method close(), I have a code as follows, could any one explain why I need to check if(input != null) please ?
Scanner input = new Scanner(new File("System.txt"));
...
Public void closeFile() {
if(input != null) {
input.close();
}
}
its just a
null checkif input is null it’d throw NullPointerException.consider below code:
now, as input is null, if you try to call close() it’d throw NPE>