Not Serializable Exception:
The java.io.notserializable 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 java.io.notserializableException:
This exception indicates that the object that you are trying to serialize is not serializable for some reasons.This exception belongs to java.io.package.This 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.
Conditions:
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.io.*;
import java.util.*;
class Parent
{
Parent()
{
}
String parentname;
Parent(String name)
{
parentname=name;
}
}
class Child1 extends Parent implements Serializable
{
Child1(String name)
{
super(name);
}
}
class Child2
{
public static void main(String args[])
{
Child1 c1=new Child1("ganesh");
Parent p=new Parent("Jesus");
try
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));
out.writeObject(c1);
out.writeObject(p);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));
Parent pa=(Parent)in.readObject();
Parent parent = (Child1) in.readObject();
in.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Exception thrown:
java.io.NotSerializableException: Parent
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Child2.main(Child2.java:35)
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 java.io.NotSerializableException thrown because of a serializable object containing a non-serializable object in the other post java.io.NotSerializableException : containing a non-serializable .
Link for that post is http://craftingjava.blogspot.in/2012/06/javaionotserializableexception_17.html
0 comments:
Post a Comment