Java编程关于 java.lang.NullPointerException的问题

package student_system_new;

public class Student {
public Student() {
this.studentID = 0;
this.Name = "";
Math = 0.0;
Chinese = 0.0;
English = 0.0;
}
public Student(int studentID, String name, double math, double chinese, double english) {
this.studentID = studentID;
this.Name = name;
Math = math;
Chinese = chinese;
English = english;
}
private int studentID;
private String Name;
private double Math;
private double Chinese;
private double English;

public void setName(String name) {
    Name = name;
}

public void setStudentID(int ID) {
    studentID = ID;
}

public void setMath(double math) {
    Math = math;
}

public void setChinese(double chinese) {
    Chinese = chinese;
}

public void setEnglish(double english) {
    English = english;
}

}

package student_system_new;

import java.util.*;

public class Classes {

public Classes() {
    super();
}

final static int SIZE = 5;
private Student[] class1 = new Student[SIZE];

public void setData() {
    System.out.println("输入学生数据:学号、姓名、数学、语文、英语");
    Scanner data = new Scanner(System.in);
    for(Student a: class1) {
        a.setStudentID(data.nextInt());
        a.setName(data.next());
        a.setMath(data.nextDouble());
        a.setChinese(data.nextDouble());
        a.setEnglish(data.nextDouble());
    }
    data.close();
}

}

package student_system_new;

public class Texter {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Classes first = new Classes();
    first.setData();

}

}

有三个类,setData目的是想输入五个学生数据,可是在输入的时候显示了一个错误。这个错误好像是跟数组有关,可是我弄了好久没搞清楚,哪位大神能帮一下....图片说明图片说明

2个回答

public void setData() {
    System.out.println("输入学生数据:学号、姓名、数学、语文、英语");
    Scanner data = new Scanner(System.in);
        for(int i=0; i<SIZE; i++) {/////////////
        class1[i]=new Student();//////////////////
        class1[i].setId(data.nextInt());
        class1[i].setName(data.next());
        class1[i].setMath(data.nextDouble());
        class1[i].setChinese(data.nextDouble());
        class1[i].setEnglish(data.nextDouble());
    }
    data.close();
}
     for(Student a: class1) {
         a=new Student();//加上这个试试
        a.setStudentID(data.nextInt());
        a.setName(data.next());
        a.setMath(data.nextDouble());
        a.setChinese(data.nextDouble());
        a.setEnglish(data.nextDouble());
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!