[英]Why am I receiving NullPointerExceptions when setting values to an object?
I've created a class Student with values of ID, GPA, and Credits, and then in class StudentList, I created 100 elements of an array. 我创建了一个具有ID,GPA和Credits值的Student类,然后在StudentList类中创建了100个数组元素。 When trying to write to an element, I get NullPointerException.
尝试写入元素时,出现NullPointerException。
Student Class: 学生班:
package com.ahellhound.pkg202studentrecordproject;
public class Student {
private String studentID;
private double studentGPA;
private int creditHours;
public void Student() {}
public String getStudentID() {return studentID;}
public void setStudentID(String studentID) {this.studentID = studentID;}
public double getStudentGPA() {return studentGPA;}
public void setStudentGPA(double studentGPA) {this.studentGPA = studentGPA;}
public int getCreditHours() {return creditHours;}
public void setCreditHours(int creditHours) {this.creditHours = creditHours;}
/**
* @return StudentID, StudentGPA, StudentCreditHours separated by spaces
*/
@Override
public String toString() {
return String.format("%s %f.2 $d", studentID, studentGPA, creditHours);
}
}
Then in StudentList, I try passing three parameters to it: 然后在StudentList中,尝试将三个参数传递给它:
private Student[] studentArray = new Student[100];
private int counter = 0;
public String addStudentRecord(String studentID, double studentGPA, int studentCredits){
studentArray[counter].setStudentID(studentID);
studentArray[counter].setStudentGPA(studentGPA);
studentArray[counter].setCreditHours(studentCredits);
String arrayString = studentArray[counter].toString();
counter++;
return arrayString;
}
This creates array which can hold up to 100 object of Student : 这将创建最多可容纳100个Student对象的数组:
private Student[] studentArray = new Student[100]
. private Student[] studentArray = new Student[100]
。
However it does not creating any students, so every single cell contains null. 但是,它不会创建任何学生,因此每个单元格都包含null。
Using this line studentArray[counter].setStudentID(studentID);
使用此行
studentArray[counter].setStudentID(studentID);
means you try to call setStudentID(studentID)
on null object, because studentArray[0]
is null. 表示您尝试对null对象调用
setStudentID(studentID)
,因为studentArray[0]
为null。
The best way how to do it is create constructor in your Student class 最好的方法是在您的Student类中创建构造函数
public Student(String studentID, double studentGPA, int creditHours) {
this.studentID = studentID;
this.studentGPA = studentGPA;
this.creditHours = creditHours;
}
And then you can add student with this : 然后您可以添加学生:
studentArray[counter] = new Student(studentID, studentGPA, creditHours);
Inside addStudentRecord, first initialize the array element to a new student: 在addStudentRecord内部,首先将array元素初始化为新学生:
studentArray[counter] = new Student();
// rest of your code
The exact issue is very clearly explained by the others, but I can provide some additional insight as to how exactly you can prevent this from ever being an issue in the first place. 确切的问题由其他人非常清楚地解释了,但是我可以提供一些其他的见解,以帮助您从一开始就如何准确地防止它成为问题。
The basic, core problem with the code, the one that caused this mishap, is the fact that studentID
is mutable. 代码的基本核心问题(导致此事故的原因)是
studentID
易变的事实。 You need to ask yourself, "is at any point, ever, the student ID of the object going to change?". 您需要问自己:“该对象的学生证在任何时候都会改变吗?”。 If your school works anything like a real one, the answer is a resolute no , and in fact, that is the entire reason for having a student ID - it is something that will never ever change.
如果您的学校的工作方式像真实学校一样,那么答案肯定是“ 否” ,实际上,这就是拥有学生证的全部原因-这是永远不会改变的。 Because of this, you should not have a setter for that field;
因此,您不应该为该字段设置setter。 it should only be initialized in the constructor.
它只能在构造函数中初始化。 (You should implement the
hashCode
method around studentID.hashCode()
, and the equals
method around studentID.equals()
.) (您应该在
studentID.hashCode()
周围实现hashCode
方法,而在studentID.equals()
周围studentID.equals()
equals
方法。)
Of course, if you can only initialize the student ID it in the constructor, then when you add a record, you will have to construct a new student object, and if you construct a new student object, then you will not have a NullPointerException
. 当然,如果只能在构造函数中初始化学生ID,则在添加记录时,您将必须构造一个新的Student对象,而如果您构造一个新的Student对象,则将没有
NullPointerException
。
So: 所以:
setStudentID
setStudentID
studentID
as a parameter to the constructor studentID
作为参数添加到构造函数中 hashCode
and equals
hashCode
equals
When you create an array of any type, you're creating a container for some set of objects. 创建任何类型的数组时,都在为某些对象集创建容器。 Java tries to make some sensible defaults for primitive types such as assigned 0 to members of an int array.
Java尝试为原始类型(例如,将0分配给int数组的成员)设置一些明智的默认值。 But for objects, the only sensible default is
null
. 但是对于对象,唯一明智的默认值为
null
。
For you, this means you need to create an object and insert it into the array before you try to access its fields. 对您来说,这意味着您需要创建一个对象并将其插入数组,然后再尝试访问其字段。
public String addStudentRecord(String studentID, double studentGPA, int studentCredits){
studentArray[counter] = new Student();
studentArray[counter].setStudentID(studentID);
studentArray[counter].setStudentGPA(studentGPA);
studentArray[counter].setCreditHours(studentCredits);
String arrayString = studentArray[counter].toString();
counter++;
return arrayString;
}
You may also want to consider what to do if studentArray[counter] is null. 您可能还需要考虑如果studentArray [counter]为null时该怎么办。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.