0

I am trying to deserialize a hashmap -

HashMap<String, Movie> map

I have defined two classes Movie and Pic, code shown below

Class Movie

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

The map is loaded using a 'loadObject' function -

@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;
} 

Hence, this code is used to load the hashmap map -

        Data.map = Data.loadObject(mappath);

But i am unable to load the map and instead getting java.io.NotSerializableException, the stacktrace is

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)

Since all classes used implement java.io.Serializable, what is causing this error ??

Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
  • Did you give us all of the fields of the two classes? One thing about `Serializable` is that objects aren't really serializable if one of their fields isn't. – Dennis Meng Jul 26 '13 at 16:27
  • There are a few functions all public and return nothing (void). Is there anything else that could cause this error ? Thanks ! – Ankit Rustagi Jul 26 '13 at 17:19

2 Answers2

6

The real problem occurred earlier when you serialized the object. The javadoc for WriteAbortedException says this:

Signals that one of the ObjectStreamExceptions was thrown during a write operation. Thrown during a read operation when one of the ObjectStreamExceptions was thrown during a write operation.

In other words, when you originally serialized the object, it threw an exception that your code ignored ... or didn't remediate properly. Now when you attempt to deserialize the object, it says you can't because the serialization did not complete properly.

Looking at the Movie and Pic classes, I can't see an obvious reason for original serialization to fail. The best I can suggest (for now) is that you fixed an earlier problem in your code base, but forgot to delete some incomplete serialized objects that were created using the earlier version.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks @Stephen ! I think may be one of the classes earlier were not implementing java.io.Serializable when the serialized file was generated. I had to delete the old serialized file and generate a new one again. Thanks again for the help ! – Ankit Rustagi Jul 27 '13 at 07:20
  • i faced a similar issue and your point of "**real problem occurred earlier**" helped a lot .. my console stacktrace was **overflowing and got real exception (original cause) got replaced in console)** – Srinath Ganesh May 01 '15 at 04:26
-4

java.io.NotSerializableException: Movie

This exception message appears to be entirely self-explanatory to me. To restate it, your Movie class doesn't implement Serializable. Perhaps you aren't running the code you think you're running, or perhaps you have different code at the sender and receiver.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • -1 Given that the OP's code has had `public class Movie implements java.io.Serializable` in it since the beginning... – Dennis Meng Jul 27 '13 at 02:29
  • There is nothing in his post about 'since the beginning'. Given that the exception says what it says, the OP is clearly mistaken. The exception does not lie. – user207421 Jul 27 '13 at 03:54
  • It does not lie, but it can mislead ... if you don't read it carefully. – Stephen C Jul 27 '13 at 04:15
  • @StephenC So what else can it mean exactly, in its 'misleading' way? – user207421 Jul 27 '13 at 10:01
  • @DennisMeng See the OP's comment under Stephen C's answer, where he basically agrees with what I've written here. You might want to reconsider your comment. There is simply no other explanation, unless you have one? – user207421 Jul 28 '13 at 00:40
  • I'll revoke the downvote, but only because you changed your answer to be more helpful. Merely stating that a certain class doesn't implement `Serializable` when the posted code says `implements java.io.Serializable` doesn't help, and is completely useless to someone who doesn't know specifics. Stating why the class might not actually be serializable *in spite of* `implements java.io.Serializable` appearing in the code *is* helpful. – Dennis Meng Jul 28 '13 at 04:41
  • @DennisMeng If all that means you consider it acceptable to downvote self-evidently correct answers, I can only say I disagree. – user207421 Jul 28 '13 at 09:50
  • @EJP No, I don't find it acceptable to downvote self-evidently correct answers. Given the code that was posted by the OP, I wouldn't call your original post "self-evidently correct" so much as "pretentious and not helpful". Your edited answer though, did provide something helpful, and so the downvote was no longer warranted. – Dennis Meng Jul 28 '13 at 15:59