我目前正在使用Java的學生數據庫,並且有兩個我想保存的特定列表。學生和個人資料(用於登錄)。我目前正在對學生進行序列化測試,只是爲了讓它工作,但一直在發生一個奇怪的問題。Java - 序列化 - NotSerializableException問題
我的學生對象類和代碼如下:
Student.java
StudentsCollection.java
學生創建我的學生對象(自我解釋)並且我的StudentsCollection()實例化了存儲我的Student對象的類型Student的列表試圖保存/載入我使用此代碼的對象,並得到以下異常拋出:
/**
* 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)
我也想指出的是,students.ser實際上是在我的項目瀏覽器中創建的,但是異常仍在拋出,即使嘗試使用openCollection()錯誤時也會拋出,但是我想首先正確解決saveCollection()
我在你的代碼片段中看不到它,但是該錯誤讀取就像它試圖序列化一個掃描器。您的學生班有沒有機會將掃描儀作爲一個領域? –
嘆息你是對的,一旦我失去了我的項目,我曾問朋友反編譯我的JAR和一些代碼混亂起來,默認的構造函數是由於某種原因請求輸入學生名稱。將嘗試刪除所有的掃描器,看看是否它的工作原理 – Juxhin
你的班級是否有'Scanner'字段? – Pshemo