运行报错,空指针异常,怎么解决
package com.XuanKe;
import java.util.List;
import java.util.ArrayList;
/**
* 备选课
* @author Administrator
*
*/
public class BeiXUanKeTest {
private List CourseToSelect;
public List getCourseToSelect() {
return CourseToSelect;
}
public void setCourseToSelect(List courseToSelect) {
this.CourseToSelect = new ArrayList();
}
public void CourseAdd(){//添加备选课程
Course cr1=new Course();
cr1.setId("1");
cr1.setName("数据结构");
CourseToSelect.add(cr1);
Course temp =(Course) CourseToSelect.get(0);
System.out.println("添加课程为:"+temp.getId()+" :"+temp.getName());
}
public static void main(String[] args) {
BeiXUanKeTest bx=new BeiXUanKeTest();
bx.CourseAdd();
}
}
package com.XuanKe;
import java.util.HashSet;
import java.util.Set;
/**
* 学生类
* @author Administrator
*
*/
public class Student {
private String id;
private String name;
private Set course;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getCourse() {
return course;
}
public void setCourse(Set course) {
this.course = new HashSet();
}
}
package com.XuanKe;
/**
* 课程类
* @author Administrator
*
*/
public class Course {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}