Я делаю базовое c приложение для управления студентами, которое запускается в консоли всего с двумя функциями: Добавить студента и показать список студентов. Но я получаю ошибки в Serializable Exception, хотя я реализую Serialization. Мои занятия, как показано ниже.
Класс ученик
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int id;
private int gender;
private float gpa;
private static final long serialVersionUID = 1L;
public Student() {}
public Student(String name, int id, int gender, float gpa) {
super();
this.name = name;
this.id = id;
this.gender = gender;
this.gpa = gpa;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int isGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public float getGpa() {
return gpa;
}
public void setGpa(float gpa) {
this.gpa = gpa;
}
}
СтуденДАО класс
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.io.SerializablePermission;
import java.util.ArrayList;
import java.util.List;
import javax.sql.rowset.serial.SerialException;
public class StudentDAO {
private static final String STUDENT_FILE_NAME = "studentRecord.txt";
/**
* save list student to file
* @param studentList: list student to save
*/
public void write(List<Student> studentList) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(new File(STUDENT_FILE_NAME));
oos = new ObjectOutputStream(fos);
oos.writeObject(studentList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeStream(fos);
closeStream(oos);
}
}
/**
* read list student from file
* @return list student
*/
public List<Student> read() throws SerialException {
List<Student> studentList = new ArrayList<>();
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(new File(STUDENT_FILE_NAME));
ois = new ObjectInputStream(fis);
studentList = (List<Student>) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
closeStream(fis);
closeStream(ois);
}
return studentList;
}
/**
* close input stream
*/
private void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* close output stream
*/
private void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
StudentManagement класс
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.sql.rowset.serial.SerialException;
public class StudentManagement {
StudentDAO stdao;
List<Student> list;
Scanner sc = new Scanner(System.in);
public StudentManagement() {
try {
stdao = new StudentDAO();
list = stdao.read();
} catch (SerialException e) {
e.printStackTrace();
}
}
public void add() {
System.out.print("Enter student id \t");
int id = sc.nextInt();
System.out.println();
System.out.print("Enter student name \t");
String name = sc.next();
System.out.println();
System.out.print("Choose gender:\t[male] or [female]?");
int gender = -1;
String tempGender = sc.next();
if(tempGender.equalsIgnoreCase("male")) {
gender = 1;
}else if(tempGender.equalsIgnoreCase("female")) {
gender = 0;
}
System.out.println();
System.out.print("Enter student GPA \t");
float gpa = sc.nextFloat();
Student student = new Student(name, id, gender, gpa);
list.add(student);
stdao.write(list);
}
//View
public void showList() {
for(Student s : list) {
System.out.printf("%5d %10s %5s %10s \n");
System.out.printf("%5d %10s %5s %10s", s.getId(), s.getName(), s.isGender(), s.getGpa());
}
}
}
И это Main class
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
StudentManagement management = new StudentManagement();
Scanner sc = new Scanner(System.in);
String choose = sc.next();
boolean quit = false;
createMenu();
while(true) {
switch(choose) {
case "1":
management.add();
break;
case "6":
management.showList();
break;
case "7":
System.out.println("QUIT");
quit = true;
break;
}
if(quit) {
break;
}
}
}
//Create menu for user's selection
public static void createMenu() {
System.out.println("----Menu----");
System.out.println("1. Add new student.");
System.out.println("2. Modify student info.");
System.out.println("3. Sort student by GPA.");
System.out.println("4. Sort student by NAME.");
System.out.println("5. Remove student.");
System.out.println("6. View.");
System.out.println("7. Exit");
}
}
Затем я получаю эти ошибки