java java.lang.NullPointerException 报错
代码:
import java.util.List;
import java.util.HashSet;
import java.util.Set;
public class FriendFinder {
protected ClassesDataSource classesDataSource;
protected StudentsDataSource studentsDataSource;
public FriendFinder(ClassesDataSource cds, StudentsDataSource sds) {
classesDataSource = cds;
studentsDataSource = sds;
}
public Set<String> findClassmates(Student theStudent) {
String name = theStudent.getName();
// find the classes that this student is taking
List<String> myClasses = classesDataSource.getClasses(name);
Set<String> classmates = new HashSet<String>();
// use the classes to find the names of the students
for (String myClass : myClasses) {
// list all the students in the class
List<Student> students = studentsDataSource.getStudents(myClass);
for (Student student : students) {
// find the other classes that they're taking
List<String> theirClasses = classesDataSource.getClasses(student.getName());
// see if all of the classes that they're taking are the same as the ones this student is taking
boolean same = true;
for (String c : myClasses) {
if (theirClasses.contains(c) == false) {
same = false;
break;
}
}
if (same) {
if (student.getName().equals(name) == false && classmates.contains(student.getName()) == false)
classmates.add(student.getName());
}
}
}
if (myClasses == null){
throw new IllegalArgumentException("Student is not taking any class");
}
else if (classmates == null){
throw new IllegalStateException("There are no students taking the same class");
}
if (classmates.isEmpty()) {
return null;
}
else return classmates;
}
}
报错:
#1. findClassmates throws java.lang.NullPointerException when ClasssesDataSource field is null
#2. findClassmates throws java.lang.NullPointerException when ClassesDataSource.get returns null for some students
#3. findClassmates throws java.lang.NullPointerException when StudentsDataSource.get returns a List containing null
#4. findClassmates throws java.lang.NullPointerException when ClassesDataSource.get returns null for input name
#5. findClassmates does not correctly handle situation in which StudentsDataSource field is null
#6. findClassmates does not correctly handle situation in which name of input Student is null
#7. findClassmates throws java.lang.NullPointerException when input is null
#8. findClassmates throws java.lang.NullPointerException when StudentsDataSource.get returns a List containing a Student with
name = null
#9. findClassmates throws java.lang.NullPointerException when StudentsDataSource.get returns null
新人,求大神指导