java.lang.NullPointerException 当我创建一个使用另一个 class 值的数组时

java.lang.NullPointerException when I create an array that uses values of an other class

我有一个 class 学生有 3 个属性,

public static class Student {
    public String firstname;
    public String lastname;
    public int studentnumber;
}

我想在外部 class 的合适循环中初始化一个数组。每个学生的属性都将使用用户输入进行初始化(因为我有一个终端 class):

public class main {
    public static void main(String[] args) {
        int numberofstudents = Terminal.askInt("How many students to you want to enter?  ");
        Student[] array = new Student[numberofstudents];

        for (int i = 0; i < numberofstudents; i++) {
            array[i].firstname = Terminal.askString("Enter student's firstname ");
            array[i].lastname = Terminal.askString("Enter student's lastname ");
            array[i].studentnumbere = Terminal.askString("Enter student's number ");
        }
    }
}

但是每次我初始化一个数组的值,

array[i].firstname = Terminal.askString("Student's firstname ");

我明白了

Exception in thread "main" java.lang.NullPointerException

您的 Student 数组为空!它有您输入的长度,但其中没有学生对象。

创建一个新的Student,并首先将其添加到列表中!

for(int i = 0;i<numberofstudents;i++) {
    array[i] = new Student();
    array[i].firstname = Terminal.askString("Enter student's firstname ");
    // ...
}

您需要用 new Student();

初始化数组的每个项目

最好有一个普通的 Student class(不是静态的)。似乎您想保留学生总数,因此您只能将该变量作为静态属性(private static int studentNumber),并且每当您创建 Student 的新实例时,只需 ++ [= 的值15=]。在这种情况下,您不需要每次都获取学生编号。

拥有 private 属性并通过 getterssetters 访问它们比 public 更好属性。

public class Student {
    private static int studentNumber = 0;
    private String firstName;
    private String lastName;
    private Long studentId;

    public Student(String firstName, String lastName, Long studentId) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.studentId = studentId;
        studentNumber++; // increase students count after each initialization
    }

    public static int getStudentNumber() {
        return studentNumber;
    }

    public static void setStudentNumber(int studentNumber) {
        Student.studentNumber = studentNumber;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Long getStudentId() {
        return studentId;
    }

    public void setStudentId(Long studentId) {
        this.studentId = studentId;
    }
}

在您的 Main class 中,您需要使用 new Student();

初始化数组的每个项目
public class Main {
    public static void main(String[] args) {
        int numberOfStudents = Terminal.askInt("How many students to you want to enter?  ");

        // this line just create an empty array that can hold Student objcets in it
        Student[] array = new Student[numberOfStudents];

        for (int i = 0; i < array.length; i++) {
            // you need to initialize each items of an array with new()
            array[i] = new Student(Terminal.askString("Enter student's firstname "),
                    Terminal.askString("Enter student's lastname "),
                    Terminal.askString("Enter student's ID "));
        }
    }
}

不要忘记遵循缩进规则,所有 classes 名称都以大写开头 (Main, 学生,终端,等等)。最后使用驼峰式 (studentNumbers, firstName, 姓氏).

您可以向 Student class 添加一个具有三个字段的 构造函数 并简化您的代码如下:

public static class Student {
    public String firstname;
    public String lastname;
    public int studentNumber;

    public Student(String firstname, String lastname, int studentNumber) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.studentNumber = studentNumber;
    }
}
public static void main(String[] args) {
    int numberOfStudents =
            Terminal.askInt("How many students to you want to enter?  ");
    Student[] array = new Student[numberOfStudents];

    for (int i = 0; i < numberOfStudents; i++) {
        array[i] = new Student(
                Terminal.askString("Enter student's firstname "),
                Terminal.askString("Enter student's lastname "),
                Terminal.askString("Enter student's number "));
    }
}

您需要先用 new Student() 初始化数组索引,然后才能更新此数组索引处的值。默认情况下,Student[] 在每个索引处包含 null,因此如果您尝试在其上执行任何操作(例如,将值赋给 array[i].firstname)而不进行初始化,您将得到 NullPointerException它将是一个非空值。

for(int i = 0;i<numberofstudents;i++){
    array[i] = new Student();
    array[i].firstname = Terminal.askString("Enter student's firstname ");
    array[i].lastname = Terminal.askString("Enter student's lastname ");
    array[i].studentnumbere = Terminal.askString("Enter student's number ");
}