我正在尝试反序列化哈希图 -
HashMap<String, Movie> map
我定义了两个类Movie和Pic,代码如下
类电影
public class Movie implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
public String title;
public Pic poster;
public Pic[] actors;
public Pic[] directors;
public Pic[] writers;
public String rating;
public String[] genres;
public String plot;
// Also contains a few getters and setters ..
}
班级图片
public class Pic implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
String name;
Boolean isDownloaded;
public Pic()
{
name="";
isDownloaded=false;
}
public Pic(String name,Boolean isdwn)
{
this.name = name;
this.isDownloaded = isdwn;
}
}
使用“loadObject”函数加载地图 -
@SuppressWarnings("unchecked")
public static HashMap<String, Movie> loadObject(String path)
{
HashMap<String, Movie> o = null;
try
{
FileInputStream fileIn = new FileInputStream(path);
ObjectInputStream in = new ObjectInputStream(fileIn);
o = (HashMap<String, Movie>) in.readObject();
in.close();
fileIn.close();
}
catch(IOException i)
{
System.out.println("\n Exception 1 while loading object \n");
i.printStackTrace();
}
catch(ClassNotFoundException c)
{
System.out.println("\n Exception 2 while loading object \n");
}
return o;
}
因此,此代码用于加载 hashmap 映射 -
Data.map = Data.loadObject(mappath);
但我无法加载地图,而是得到 java.io.NotSerializableException,堆栈跟踪是
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: Movie
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1331)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:349)
at java.util.HashMap.readObject(HashMap.java:1030)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:969)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1775)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1327)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:349)
at Data.loadObject(Data.java:69)
at gui.main(gui.java:113)
Caused by: java.io.NotSerializableException: Movie
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at java.util.HashMap.writeObject(HashMap.java:1001)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:940)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at Data.saveObject(Data.java:45)
at Data.sync(Data.java:103)
at gui$3.doInBackground(gui.java:618)
at gui$3.doInBackground(gui.java:1)
at javax.swing.SwingWorker$1.call(SwingWorker.java:277)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at javax.swing.SwingWorker.run(SwingWorker.java:316)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
由于所有使用的类都实现了 java.io.Serializable,是什么导致了这个错误??