为什么会出现java.lang.nullpointerexception错误
public void testSetContains(){
//提示输入课程名称
System.out.println("请输入学生已选的课程名称:");
String name=console.next();
//创建一个新的课程对象,ID和名称,与course对象完全一样
Course course2=new Course();
course2.name=name;
System.out.println("新创建课程:"+course2.name);
System.out.println("备选课程中是否包含课程:"+course2.name+","+student.courses.contains(course2));
}
执行到画线句出现错误,问题应该在student.courses.contains(course2),但是已经定义public Student student; 而且
public class Student {
public String id;
public String name;
public Set<Course> courses;//Set和List一样都是Collection的子接口
public Student(String id,String name){
this.id=id;
this.name=name;
this.courses=new HashSet<Course>();
}
}