I'm currently working on a student database using Java only and have two particular lists I would like to save. Students and Profiles(To login with). I'm currently testing out serialization on Students only to get it to work but have been getting a weird issue.
My Student object classes and code are as follows:
Student.java
StudentsCollection.java
Students creates my Student object(Self explanatory) and my StudentsCollection() instantiates a list of type Student which stores my Student objects, when trying to save/load the objects I use this code and get the following exception thrown:
/**
* Save student collection
*/
public void saveCollection(){
try {
FileOutputStream e = new FileOutputStream("students.ser");
ObjectOutputStream outputStream = new ObjectOutputStream(e);
for(Students i : this.list){
outputStream.writeObject(i);
}
outputStream.flush();
outputStream.close();
} catch (IOException var3) {
var3.printStackTrace();
JOptionPane.showMessageDialog((Component)null, "Error. Cannot save database.");
}
}
/**
* Open student collection
*/
public void openCollection(){
try {
FileInputStream e = new FileInputStream("students.ser");
ObjectInputStream inputSteam = new ObjectInputStream(e);
while(inputSteam.readObject() != null){
this.list.add((Students)inputSteam.readObject());
}
} catch (FileNotFoundException var3) {
var3.printStackTrace();
JOptionPane.showMessageDialog(null, "File not found");
} catch (IOException var4) {
var4.printStackTrace();
JOptionPane.showMessageDialog(null, "IO Exception");
} catch (ClassNotFoundException var5) {
var5.printStackTrace();
JOptionPane.showMessageDialog(null, "Required class not found");
}
}
And the following exception is printed:
java.io.NotSerializableException: java.util.Scanner
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at jdatabase.objects.students.StudentsCollection.saveCollection(StudentsCollection.java:539)
at jdatabase.main.MainController$1.run(MainController.java:22)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
I would also like to point out that students.ser is actually created in my project explorer but exceptions are still being thrown, even when trying to use openCollection() errors are thrown, however I'd like to tackle saveCollection() properly first