java java.lang.NullPointerException 报错

qq_40545705 2018-06-13 01:53:37
代码:
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


新人,求大神指导
...全文
969 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
stacksoverflow 2018-06-13
  • 打赏
  • 举报
回复
不知道你这工程是包在什么框框里的,这样吧,findClassmates中打印一些信息,然后贴出来。 public Set<String> findClassmates(Student theStudent) { System.out.println("classesDataSource"+classesDataSource); System.out.println("studentsDataSource"+studentsDataSource); System.out.println("theStudent"+theStudent); System.out.println("theStudent.getName()"+theStudent.getName());
qq_40545705 2018-06-13
  • 打赏
  • 举报
回复
引用 1 楼 stacksoverflow 的回复:
1. 报错的行号没有显示,不好定位。 2. 貌似是StudentsDataSource, ClassesDataSource查询时返回空报的异常。 需要提供更详细的日志信息,和StudentsDataSource, ClassesDataSource代码。
如果想要throw这些报错信息应该怎么处理?
stacksoverflow 2018-06-13
  • 打赏
  • 举报
回复
1. 报错的行号没有显示,不好定位。 2. 貌似是StudentsDataSource, ClassesDataSource查询时返回空报的异常。 需要提供更详细的日志信息,和StudentsDataSource, ClassesDataSource代码。

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧