java.io.NotSerializableException - 无法在Java中反序列化hashmap

时间:2013-07-26 16:00:39

标签: java exception serialization

我正在尝试反序列化一个hashmap -

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 ..
}

Class Pic

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 map -

        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,导致此错误的原因是什么?

2 个答案:

答案 0 :(得分:5)

序列化对象时,实际问题就出现了。 WriteAbortedException的{​​{3}}说明了这一点:

  

表示在写入操作期间抛出了ObjectStreamExceptions之一。在写入操作期间抛出ObjectStreamExceptions之一时,在读取操作期间抛出。

换句话说,当您最初序列化对象时,它会抛出一个异常,即代码被忽略......或者没有正确修复。现在,当您尝试反序列化对象时,它表示您不能,因为序列化没有正确完成。

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

答案 1 :(得分:-3)

  

java.io.NotSerializableException:Movie

此异常消息对我来说似乎完全不言自明。要重述它,您的Movie类不实现Serializable。也许您没有运行您认为正在运行的代码,或者您可能在发送方和接收方处拥有不同的代码。