因此,我有兩個聲明爲靜態的類(通過使它們的方法爲靜態,並且它們的構造函數爲private),Synchronizer和UserProfile。同步的SAVEDATA()函數調用用戶配置的SAVEDATA()函數:使用靜態方法執行文件I/O時的NotSerializableException
private static void saveData(){
UserProfile.saveData(data, dataFile);
}
這裏是UserProfile.saveData(...):
public static void saveData(Object obj, File file){
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(obj);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
同步器和用戶配置都實現Serializable接口,並有一個自動生成的serialVersionUID。
任何幫助?我很確定我正在做一些根本性的錯誤。
編輯:這裏是輸出。
Restoring sync data
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: org.jsoup.nodes.Document
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at java.util.ArrayList.readObject(ArrayList.java:733)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1004)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at UserProfile.restoreData(UserProfile.java:93)
at Synchronizer.restoreData(Synchronizer.java:74)
at Processor.<init>(Processor.java:8)
at CommandLine.<init>(CommandLine.java:15)
at TuneSnatch.main(TuneSnatch.java:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.io.NotSerializableException: org.jsoup.nodes.Document
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at java.util.ArrayList.writeObject(ArrayList.java:710)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at UserProfile.saveData(UserProfile.java:80)
at Synchronizer.saveData(Synchronizer.java:87)
at Synchronizer.addHTML(Synchronizer.java:44)
at Processor.sync(Processor.java:100)
at CommandLine.launch(CommandLine.java:99)
at TuneSnatch.main(TuneSnatch.java:6)
... 5 more
它意味着「數據」,無論它真的是什麼類,都是不可序列化的。 –
應該是不可序列化的'org.jsoup.nodes.Document'(在'ArrayList'中)。您想要序列化的對象需要支持它,而不是啓動序列化的代碼。 – zapl