空指針異常
package com.hujinming.test;
public class Course {
?? ?public String id;
?? ?public String name;
?? ?public? Course(String id , String name) {
?? ??? ?this.id = id;
?? ??? ?this.name = name;
?? ??? ?System.out.println("執(zhí)行了!");
?? ?}
}
package com.hujinming.test;
import java.util.ArrayList;
import java.util.List;
public class CourseToStudent {
?? ?public List courseToSelect;
?? ?
?? ?public void CourseToStudent() {
?? ??? ?this.courseToSelect = new ArrayList();
?? ?}
?? ?/**
?? ? *
?? ? * 用于往備選課程里面添加課程
?? ? */
?? ?public void courseAdd() {
?? ??? ?Course crl = new Course("1","高數(shù)");?
?? ??? ?courseToSelect.add(crl);//這兒出現(xiàn)空指針異常
?? ???? Course temp = (Course) courseToSelect.get(0);
?? ??? ?System.out.println("添加了課程"+":"+temp.id+","+temp.name);
?? ??? ?
?? ?}
}
2022-03-23
路過,也踩!ls顯示目錄內(nèi)容
2017-12-27
public void CourseToStudent() {
?? ??? ?this.courseToSelect = new ArrayList();
?? ?}
構(gòu)造函數(shù)應(yīng)該是public ?CourseToStudent()
2017-12-27
奧 知道了? 構(gòu)造函數(shù)不能帶有void