为了账号安全,请及时绑定邮箱和手机立即绑定

学生选课--Set集合管理

Exception in thread "main" java.lang.NullPointerException

at studentInfo.setTest.testAdd(setTest.java:21)

at studentInfo.setTest.main(setTest.java:49)

按老师的代码运行有错  看了半天不知道哪里出问题  求大神 指点

正在回答

2 回答

public void testAdd(){

Course cr=new Course("1","c sharp");

courseToSelect.add(cr);

Course temp=(Course)courseToSelect.get(0);

System.out.println("the add test is: "+cr.id+":"+cr.name);

Course cr1=new Course("2","my sql");

courseToSelect.add(0, cr1);

Course temp1=(Course)courseToSelect.get(1);

System.out.println("the add test is: "+cr1.id+":"+cr1.name);

Course[] cse={new Course("3","data struct"),new Course("4","orlce")};

courseToSelect.addAll(Arrays.asList(cse));

Course temp2=(Course)courseToSelect.get(2);

Course temp3=(Course)courseToSelect.get(3);

System.out.println("the add test is: "+temp2.id+":"+temp2.name);

System.out.println("the add test is: "+temp3.id+":"+temp3.name);

Course[] cse1={new Course("5","daxue english"),new Course("6","acess")};

courseToSelect.addAll(3,Arrays.asList(cse1));

Course temp4=(Course)courseToSelect.get(3);

Course temp5=(Course)courseToSelect.get(4);

System.out.println("the add test is: "+temp4.id+":"+temp4.name);

System.out.println("the add test is: "+temp5.id+":"+temp5.name);

Course cr2=new Course("1","c sharp");

courseToSelect.addAll(6,Arrays.asList(cr2));

Course temp0=(Course)courseToSelect.get(6);

System.out.println("the add test is: "+cr2.id+":"+cr2.name);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

setTest st=new setTest();

st.testAdd();

st.testForeach();

stu s=new stu("3","Slince");

System.out.println("welcome student"+s.name+"select course");

Scanner shuru=new Scanner(System.in);

for(int i=0;i<3;i++){

System.out.println("input course ID");

String str=shuru.next();

for(Course cc:st.courseToSelect){

if(cc.equals(str)){

s.courses.add(cc);

}

}

}

st.setTestforeachforset(s);

}

代码出错提示部分

错误提示

Exception in thread "main" java.lang.NullPointerException

at studentInfo.setTest.testAdd(setTest.java:21)

at studentInfo.setTest.main(setTest.java:49)


0 回复 有任何疑惑可以回复我~

把代码贴上来!

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

学生选课--Set集合管理

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信