Reputation: 5630
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
Upvotes: 0
Views: 2240
Reputation: 197
Since scanner class does not implement Serializable interface, there must be some field in your class asking for input through scanner, hence the error.
Upvotes: 0
Reputation: 3016
You can serialize an object only if all of its class members are also implementing Serializable. As RobAu mentioned, Scanner is not serializeable.
You can find a good tutorial here: http://www.vogella.com/tutorials/JavaSerialization/article.html
All fields marked with transient will not be serialized. See here for more details: How does marking a field as transient make it possible to serialise an object
Upvotes: 1
Reputation: 8587
You did not post your Student.java
, so I am guessing here. Is there a field of type java.util.Scanner
in the Student
class? If there is, delete it, or mark it as transient
:
private transient Scanner someField;
The transient
modifier will prevent the field from being serialized.
Upvotes: 5
Reputation: 20099
There is an Object Students
in your this.list
that references the Scanner
. Remove the reference and you will be fine.
Upvotes: 1