What one of the following is best practice to handle Null Pointer exception?
Posted: Wed Jul 13, 2022 7:50 pm
i) int noOfStudents = line.listStudents().count;
ii) int noOfStudents = getCountOfStudents(line);
public int getCountOfStudents(List line) { if(line != null) { if(line.listOfStudents() != null) { return line.listOfStudents().size(); } } throw new NullPointerException("List is empty"); }
a) Option (i)
b) Option (ii)
c) Compilation Error
d) Option (ii) gives incorrect result
ii) int noOfStudents = getCountOfStudents(line);
public int getCountOfStudents(List line) { if(line != null) { if(line.listOfStudents() != null) { return line.listOfStudents().size(); } } throw new NullPointerException("List is empty"); }
a) Option (i)
b) Option (ii)
c) Compilation Error
d) Option (ii) gives incorrect result