Java - 序列化 - NotSerializableException问题 [英] Java - Serialization - NotSerializableException Issue
问题描述
我目前正在使用仅使用Java的学生数据库,并且有两个我想要保存的特定列表。学生和个人资料(登录)。我目前正在测试学生的序列化只是为了让它运作起来但却遇到了一个奇怪的问题。
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.
我的学生对象类和代码如下:
Student.java
Student.java
StudentsCollection.java
StudentsCollection.java
学生创建我的学生对象(自解释),我的StudentsCollection()实例化一个列表输入存储我的Student对象的Student,当我尝试保存/加载对象时,我使用此代码并抛出以下异常:
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");
}
}
并打印以下异常:
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)
我也会喜欢指出student.ser实际上是在我的项目资源管理器中创建的,但例外情况是仍被抛出,即使在尝试使用openCollection()时也会抛出错误,但是我想首先正确处理saveCollection()
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
推荐答案
您没有发布 Student.java
,所以我猜这里。 Student
类中是否有 java.util.Scanner
类型的字段?如果有,删除它,或将其标记为 transient
:
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;
transient
修饰符将阻止该字段被序列化。
The transient
modifier will prevent the field from being serialized.
这篇关于Java - 序列化 - NotSerializableException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!