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;
}
}
package com.XuanKe; import java.util.List; import java.util.ArrayList; /** * 备选课 * @author Administrator * */ public class BeiXUanKeTest { private List CourseToSelect=new ArrayList();//直接初始化,简单明了,不会报异常 //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; } }
不用谢
你的备选可类中的CourseToSelect没有初始化,解决方案:添加一个构造器来初始化或者直接在main方法里初始化