Add to Google Reader or Homepage

Not Serializable Exception:

The exception  is one of the common exception experienced by the java programmers who uses object serialization and de-serialization concepts.In this post let us discuss about the object not serializable exception.

What is meant by

This exception indicates that the object that you are trying to serialize is not serializable for some reasons.This exception belongs to exception is thrown while serialization of objects are performed. Let us see the two important reason for which this exception is thrown.

When this notserializable Exception is thrown?

As i have told before there are two important reason for which this exception is thrown. Let us consider first and  the most important  reason for which this exception is thrown.

The first reason for which an object becomes not serializable is that  the class of the object do not implements the serializable interface.


It should be noted that if we want to serialize an object of class then that class must implement the serializable  interface or it must extend the class which implements the serializable interface.

If the above mentioned conditions are not met then the objects cannot be serialized.

How to fix the not serializable exception?

1. Make the class of the object which you are trying to serialize to implement the Serializable() interface.

2.Otherwise extend the class which implements the Serializable() interface.

Example situation for this exception:

import java.util.*;
class Parent
String parentname;
Parent(String name)

class Child1 extends Parent implements Serializable
Child1(String name)

class Child2
public static void main(String args[])
Child1 c1=new Child1("ganesh");
Parent p=new Parent("Jesus");

          ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));

            ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));
           Parent pa=(Parent)in.readObject();
           Parent parent = (Child1) in.readObject();

        catch (Exception e)

Exception thrown: Parent
        at Source)
        at Source)
        at Child2.main(

This Exceptions shows that i have tried to serialize the instance of the Parent class which does not implements the serializable interface in the class Child2.

 I have highlighted the statements which causes this Exceptions in Bold in the program.

Note that the class Parent does not implements the serializable interface which is the sole reason this exception.This exception can be avoided by making the class Parent to implement the Serializable interface.

I have dealt with the  thrown because of  a serializable object containing a non-serializable object in the other post : containing a non-serializable .

java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top