What one of the following is best practice to handle Null Pointer exception? i) int noOfStudents = line.listStudents().count; ii) int noOfStuden

Nguyễn Hào

New member
What one of the following is best practice to handle Null Pointer exception?
i) int noOfStudents = line.listStudents().count;
ii) int noOfStudents = getCountOfStudents(line);
Code:
    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
 

Tìm kiếm

Back
Top