Colby
Colby

Reputation: 313

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. When trying to write to an element, I get 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:

    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;

}

Upvotes: 0

Views: 72

Answers (4)

AJMansfield
AJMansfield

Reputation: 4129

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. 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; it should only be initialized in the constructor. (You should implement the hashCode method around studentID.hashCode(), and the equals method around studentID.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.

So:

  • Delete setStudentID
  • Add studentID as a parameter to the constructor
  • Impelement hashCode and equals

Upvotes: 1

munk
munk

Reputation: 12983

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. But for objects, the only sensible default is 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.

Upvotes: 0

Omer
Omer

Reputation: 704

Inside addStudentRecord, first initialize the array element to a new student:

studentArray[counter] = new Student();
// rest of your code

Upvotes: 1

libik
libik

Reputation: 23029

This creates array which can hold up to 100 object of Student :

private Student[] studentArray = new Student[100].

However it does not creating any students, so every single cell contains null.

Using this line studentArray[counter].setStudentID(studentID); means you try to call setStudentID(studentID) on null object, because studentArray[0] is null.

The best way how to do it is create constructor in your Student class

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

Upvotes: 3

Related Questions