Add to Google Reader or Homepage

How to handle java.io.notserializableexception: containing a non serializable object

          In this post i am going to deal with the object not serializable exception thrown because of containing a non-serializable object.This is another important reason for this exception to be thrown. i have already dealt with this exception thrown because of not implementing the serializable interface.


For the other post,go to the link mentioned below: 

 

What is meant by a non-serializable object?  


An object of a class that does not implement the serializable interface is called a non-serializable object. For example, you could have created an object for a class that does not implement the serializable interface,like


class Sample // A class not implementing the serializable interface
{
......

.........
}



Sample sampleobject=new Sample(); //Here 'sampleobject' is a non-serializable object.There are certain classes exists in the JDK library which cannot be serialized by default.

-->

 

When this exception is thrown?

If a class that implements the serializable interface contains statements that refers to an non-serializable object,then the  object of that class becomes non-serializable even though it implements the Serializable() interface and if you try to serialize the object then  this exception will be thrown.

 

How to fix this exception?

One solution that i suggest you is to  make the non-serializable object into a serializable one by making the class of the non-serializable object to implement the Serializable() interace or remove the reference to that object.

 

Example situations for this exception to be thrown:


import java.io.*;
import java.util.*;
class Parent
{

Parent()

{

}

String parentname;

Parent(String name)
{
parentname=name;
}
}

class Child1 extends Parent implements Serializable
{

Otherchild oc=null;

Child1(String name)
{
super(name);
oc=new Otherchild(name);// Containing(or creating) an isntance of a class "Otherchild" 
                                                                       that do not  implements the Serializable interface 
}
}


class Child2
{
public static void main(String args[])
{
Child1 c1=new Child1("ganesh");//trying to serialize the object of Child1 class
try
{

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

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

}

catch (Exception e)
{
e.printStackTrace();
}
}
}

class Otherchild
{
String oname;

Otherchild(String name)
{
oname=name;
}

public String getname()
{
return oname;
}
}

Exception thrown:

java.io.NotSerializableException: Otherchild

        at java.io.ObjectOutputStream.writeObject0(Unknown Source)

        at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)

        at java.io.ObjectOutputStream.writeSerialData(Unknown Source)

        at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)

        at java.io.ObjectOutputStream.writeObject0(Unknown Source)

        at java.io.ObjectOutputStream.writeObject(Unknown Source)

        at Child2.main(Child2.java:38)



In the above program i have tried to serialize the "object c1 of the class Child1 which implements the Serializable interface". But unfortunately I got the java.io.Not Serializable Exception.

Later i found that this exception is thrown because the class Child1 contains a statement that refers to an non-serializable object like this,

oc=new Otherchild(name);// Containing(or creating) an isntance of a class "Otherchild" 
                                                                       that do not  implements the Serializable interface 



This exception can be avoided by  making the non-serializable object into a serializable  object. Here,

Serializable Object -----> c1 of class Child1. 
Non-Serializable Object --------> oc of class Otherchild.


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