问答详情
源自:4-11 学生选课---通过 Set 集合管理课程

学生选课--Set集合管理

Exception in thread "main" java.lang.NullPointerException

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

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

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

提问者:缘来是你 2016-05-29 22:13

个回答

  • 缘来是你
    2016-05-30 18:01:39

    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)


  • Aioria_
    2016-05-30 00:58:20

    把代码贴上来!