What one of the following is best practice to handle Null Pointer exception?
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");
}
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
Answer: Option B
Join The Discussion