問一個小問題,我for循環那里出現了null pointer我檢查了幾遍都沒出問題
package mxxx;
public class duoduiduo {
public static void main(String[] args) {
// TODO Auto-generated method stub
? ?
? //學生信息
? ? ? ? ? ? ? ?Student stu1=new Student(1,"張小跳",18);
? ? ? ? ? ? ? ?Student stu2=new Student(2,"倩倩",19);
? ? ? ? ? ? ? ?Student stu3=new Student(3,"小洛",15);
? ? ? ? ? ? ? ?//課程信息
? ? ? ? ? ? ? ?Course ca=new Course(1122,"政治思想",3);
? ? ? ? ? ? ? ?Course cb=new Course(1125,"高等數學",4); ?
? ? ? ? ? ? ? ?//stu1和stu3選了課程ca分別的78和87分
? ? ? ? ? ? ? ?ca.setStudentCourse(new StudentCourse[]{new StudentCourse(stu1,ca,78),new StudentCourse(stu3,ca,87)});
? ? ? ? ? ? ? ?//stu2和stu3選了課程cb,分別的84和68分
? ? ? ? ? ? ? ?cb.setStudentCourse(new StudentCourse[]{new StudentCourse(stu2,cb,84),new StudentCourse(stu3,cb,68)});
? ? ? ? ? ? ? ?//stu1選了ca課程得78分
? ? ? ? ? ? ? ?stu1.setStudentCourse(new StudentCourse[]{new StudentCourse(stu1,ca,78)});
? ? ? ? ? ? ? ?//stu2選了cb課程得84分
? ? ? ? ? ? ? ?stu2.setStudentCourse(new StudentCourse[]{new StudentCourse(stu2,cb,84)});
? ? ? ? ? ? ? ?//stu3選了ca和cb課程分別得87和68分;
? ? ? ? ? ? ? ?stu3.setStudentCourse(new StudentCourse[]{new StudentCourse(stu3,ca,87),new StudentCourse(stu3,cb,68)});
? ? ? ? ? ? ? //課程信息
? ? ? ? ? System.out.println(stu3.getstudentInfo()); ? //輸出學生1得信息
? ? ? ? ? for(int i=0;i<stu3.getStudentCourse().length;i++){
? ? ? ? ?System.out.print(stu3.getStudentCourse()[i].getCourse().getCourseInfo());
? ? ? ? ?System.out.println(",成績"+stu3.getStudentCourse()[i].getScore());
? ? ? ? ? }
}
}
class Student{
private int stuId;
private String stuName;
private int age;
private StudentCourse studentCourse []; ?//這個里面包括學生信息和課程信息,所以學生和課程不需要互相自定義類型
public Student (int stuId,String stuName,int age){
this.stuId=stuId;
this.stuName=stuName;
this.age=age;
}
public void setStudentCourse(StudentCourse studentCouse[]){
this.studentCourse=studentCourse;
}
public StudentCourse[] getStudentCourse(){
return this.studentCourse;
}
public String getstudentInfo(){
return "學號:"+stuId+",姓名:"+stuName+",年齡:"+age;
}
}
class Course{
private int cid; //課程編號
private String cName;
private int credit; ?//學分
private StudentCourse studentCourse [];
public Course(int cid,String cName,int credit){
this.cid=cid;
this.cName=cName;
this.credit=credit;
}
public void setStudentCourse(StudentCourse studentCouse[]){
this.studentCourse=studentCourse;
}
public StudentCourse[] getStudentCourse(){
return this.studentCourse;
}
public String getCourseInfo(){
return "課程編號:"+cid+",課程名稱:"+cName+",課程學分:"+credit;
}
}
class StudentCourse{
private Student student;//學生信息
private Course course; ?//課程信息
private double score; ? //成績
public StudentCourse(){};
public StudentCourse(Student student,Course course,double score){//這個為student和course賦值,所以這個不需要set方法
this.student=student;
this.score=score;
this.course=course;
}
public String getStudentCourseInfo(){
return "學生信息"+student+",課程信息"+course+",成績:"+score;
}
//下面這個我認為這個是點睛之筆
public Student getStudent(){
return this.student;
}
public Course getCourse(){
return this.course;
}
? public double getScore(){
?return this.score;
? }
}
2018-08-08
public void setStudentCourse(StudentCourse studentCouse[]){
this.studentCourse=studentCourse;
}
就這段,兄嘚,你的方法中的參數數據studentCourse少了個r
2018-03-20
看了半天,沒看出問題