I am trying to add grades to a student object, this student object then has a list of these grades however I get an error
Exception in thread "main" java.lang.NullPointerException
at Student.addGrade(Student.java:24)
at Test.main(Test.java:28))
I cannot find where my logic defeats me. This is my train of thought.
The Student class has a List gradeList and a method addGrade(Grade gr) which adds gr to the gradeList, Grade is a separate class object.
In the main I create a student object, add a new grade to the student, this grade is added to a list (addGrade Method) which is unique to that student.
This clearly is not the case and I simply cannot find why, can someone please point out my folly?
Test Class
import java.time.LocalDate;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Test {
private static final String filepath="C:\\Users\\brads\\Desktop\\Serialized_Assignment3_File";
public static void main(String[]args){
Test test1 = new Test();
Student s1 = new Student(121209, "John Smith", "Newsdale park", "Comp Sci", LocalDate.of(2000, 04, 10));
test1.WriteStudentObjectToFile(s1);
Student s2 = new Student(158009, "Sarah Byrne", "Shallow road", "Physics", LocalDate.of(2000, 01, 19));
test1.WriteStudentObjectToFile(s2);
s1.addGrade(new Grade("ICT343", LocalDate.of(2020, 6, 2), (short) 85));
s1.addGrade(new Grade("ECC421", LocalDate.of(2020, 6, 6), (short) 73));
s1.addGrade(new Grade("EGG101", LocalDate.of(2020, 6, 10), (short) 59));
s1.addGrade(new Grade("EG101", LocalDate.of(2020, 6, 8), (short) 88));
s1.addGrade(new Grade("EG101", LocalDate.of(2020, 6, 14), (short) 60));
s2.addGrade(new Grade("ICT343", LocalDate.of(2020, 6, 2), (short) 58));
s2.addGrade(new Grade("ECC421", LocalDate.of(2020, 6, 6), (short) 91));
s2.addGrade(new Grade("EGG101", LocalDate.of(2020, 6, 10), (short) 59));
s2.addGrade(new Grade("EG101", LocalDate.of(2020, 6, 8), (short) 74));
s2.addGrade(new Grade("EG101", LocalDate.of(2020, 6, 14), (short) 80));
}
public void WriteStudentObjectToFile(Object serObj){
try{
FileOutputStream fileOut = new FileOutputStream(filepath);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(serObj);
objectOut.close();
System.out.println("The Object was succesfully written to a file");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
Student Class
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
public class Student implements Serializable {
private int id;
private static final long serialVersionUID = 1L;
private String name;
private String address;
private String course;
private LocalDate dateOfBirth;
private List<Grade> gradeList;
public Student(int id, String name, String address, String course, LocalDate dateOfBirth){
this.id = id;
this.name = name;
this.address = address;
this.course = course;
this.dateOfBirth = dateOfBirth;
}
public void addGrade(Grade gr){
gradeList.add(gr);
}
public List<Grade> getGrades(){
return gradeList;
}
@Override
public String toString() {
return new StringBuffer("Name: ").append(this.name).append(" Address : ").append(this.address).append(" Course : ").append(this.course).toString();
}
}
Grade Class
import java.time.LocalDate;
public class Grade {
private String moduleCode;
private LocalDate date;
private Short percentage;
public Grade(String moduleCode, LocalDate date, Short percentage){
this.moduleCode = moduleCode;
this.date = date;
this.percentage = percentage;
}
}