当我创建一个使用其他类的值的数组时

tjrkku2a  于 2021-06-30  发布在  Java
关注(0)|答案(4)|浏览(261)

我有一个班的学生有三个属性,

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

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

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
j2datikz

j2datikz1#

您需要使用 new Student() 在更新此数组索引处的值之前。默认情况下, Student[] 包含 null 在每个索引中 NullPointerException 如果您尝试执行任何操作(例如,为 array[i].firstname )在没有初始化的情况下,它将是一个非空值。

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 ");
}
1l5u6lss

1l5u6lss2#

您需要使用 new Student(); 最好有一个正常的学生班(而不是静态班)。似乎您想要保存学生总数,所以只能将该变量作为静态属性( private static int studentNumber ),并且每当您创建一个新的student示例时 ++ 价值 studentNumber . 在这种情况下,您不需要每次都获得学生编号。
最好是 private 属性,并通过getter和setter而不是 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;
    }
}

在主类中,需要使用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 "));
        }
    }
}

别忘了遵循缩进规则,所有的类名都以大写字母开头(main、student、terminal等)。最后使用camel case(studentnumbers,firstname,lastname)。

waxmsbnn

waxmsbnn3#

你的 Student 数组为空!它有您输入的长度,但其中没有student对象。
创建新的 Student ,并首先将其添加到列表中!

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

sz81bmfz4#

可以将包含三个字段的构造函数添加到 Student 将代码分类并简化如下:

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 "));
    }
}

相关问题