问一个小问题,我for循环那里出现了null pointer我检查了几遍都没出问题

来源:12-1 综合练习

斗胆请问大侠

2018-03-19 22:28

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;

  }

}


写回答 关注

2回答

  • qq_残梦g_03767056
    2018-08-08 21:40:54
    已采纳

    public void setStudentCourse(StudentCourse studentCouse[]){

    this.studentCourse=studentCourse;

    }

    就这段,兄嘚,你的方法中的参数数据studentCourse少了个r

    斗胆请问大侠

    非常感谢!

    2018-08-17 22:08:32

    共 1 条回复 >

  • 慕姐2893314
    2018-03-20 08:33:11

    看了半天,没看出问题

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

531024 学习 · 6160 问题

查看课程

相似问题