I have a student class has a name,id and an arraylist of modules. I then have an arraylist of students that I write to a txt file. When I initially create a studentList using the constructor to pass the name and ID it saves just fine but when I update the student list and add some modules to the students it gave me a seriazable error
Student Class:
`public class Student implements Serializable{
//each student has an arraylist of modules
private String name = null; //students name
private int ID = 0; //students ID number
private String DOB = null; //Students Date of Birth
private ArrayList <Module> modules; //creating the students module arraylist
public Student(String name,int ID, String DOB) {
this.setName(name);
this.setID(ID);
this.setDOB(DOB);
setModules(new ArrayList <Module> ()); //initialising the arraylist
}
`
StudentList Class:
package application;
import java.util.ArrayList;
import java.util.List;
//student arraylist
public class StudentList {
private ArrayList <Student> students; //creating the arraylist
//constructor
public StudentList() {
students = new ArrayList <Student> (); //initialising the arraylist
}
public Student findStudent (Student student) {
for(Student st: students) {
if(st.equals(student)) {
return st;
}
}
return student;
}
//getter
public ArrayList<Student> getList (){
return students;
}
public void addStudent(Student stu)
{
students.add(stu);
}
public void removeStudent(Student stu) {
students.remove(stu);
}
//return an element of the students array
public Student getStudent(int i)
{
if ((i>-1) && (i < students.size())) //making sure a valid index was entered
return students.get (i);
return null;
}
//getter for the size of the array
public int getSize (){
return students.size();
}
//method to clear the arraylist
public void clear() {
students.clear();
}
//method to add a list of type Student
public void addAll(List<Student> students2) {
for(Student student: students2) {
students.add(student);
System.out.println("Added " + student);
}
}
public Student returnElement(int i) {
Student element = students.get(i);
return element;
}
}
Controller:
package application;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class StudentController {
private StudentList stu; //creating instance of the StudentList class
//constructor
public StudentController() {
stu = new StudentList();
}
//adding a student to this list
public void addStudentToList(Student s) {
stu.addStudent(s);
}
public int getSize() { //getting the size of the arraylist
return stu.getSize();
}
public void addStudentToList(String studentName,int studentID, String studentDOB)
{
Student student = new Student(studentName, studentID, studentDOB);
stu.addStudent(student); //adding the student to the student list
}
//add students module
public void addModule(String name, int grade, Student student) {
Module module = new Module(name,grade);
stu.findStudent(student).addModule(module); //adding the module to that Student
stu.addStudent(stu.findStudent(student)); //now adding that student to our arrayliust]
}
public void saveMethod() { //method to save to the text file
try {
File file = new File("StudentData.txt");
//File to store student data
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
os.writeObject(stu.getList());
os.close();
}
catch(FileNotFoundException e1){
e1.printStackTrace();
}
catch(IOException e1) {
e1.printStackTrace();
}
}
public void load() { //method to load the contents of the text file onto my arraylist
try {
FileInputStream fis = new FileInputStream("StudentData.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
List<Student> students = (List<Student>) ois.readObject();
stu.clear();
stu.addAll(students);
ois.close();
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
public Student returnElement(int i) {
return stu.getStudent(i);
}
//getter for studentList
public String getListStudent()
{
String allStudents="\0";
for (int i = 0;i<stu.getSize();i++) //looping through the array
{
allStudents = allStudents+stu.getStudent(i);
}
return allStudents;
}
public void removeStudent(int ID) {
Student studentToRemove = null;
for(Student student:stu.getList()){ //iterate over the student list
if(student.getID()==ID) { //compring ID entered to the ID in student arraylist
studentToRemove=student;
stu.removeStudent(studentToRemove); //then remove that student
}
}
}
}
The save method is what gives this error: java.io.NotSerializableException: application.Module at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1193)