0

我正在尝试反序列化哈希图 -

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,是什么导致了这个错误??

4

2 回答 2

6

当您序列化对象时,真正的问题发生得更早。javadoc for是这样说的WriteAbortedException

ObjectStreamExceptions在写入操作期间抛出其中一个的信号。在写入操作期间抛出其中一个时在读取操作期间ObjectStreamExceptions抛出。

换句话说,当您最初序列化对象时,它引发了您的代码忽略的异常......或者没有正确修复。现在,当您尝试反序列化对象时,它说您不能,因为序列化没有正确完成。

查看MovieandPic类,我看不出原始序列化失败的明显原因。我能建议的最好的(目前)是您修复了代码库中的一个早期问题,但忘记删除使用早期版本创建的一些不完整的序列化对象。

于 2013-07-27T04:10:57.140 回答
-3

java.io.NotSerializableException:电影

这个异常消息对我来说似乎是完全不言自明的。重申一下,您的 Movie 类没有实现 Serializable。也许您没有运行您认为正在运行的代码,或者您在发送方和接收方有不同的代码。

于 2013-07-27T02:02:32.053 回答