Add to Google Reader or Homepage

java.io.InvalidClassException: no valid constructor

Invalid Class Exception: no valid constructor

The Invalid class exception is one of the commonly experienced exception by the java programmers who use object serialization in their program.There are three main causes for this exception to be thrown.They are,
1.serial version of the class
2.containing unknown data types,
3.no-arg constructor.
In this post we will discuss about the third reason "No-arg constructor".How the absence of a no-arg constructor causes this exception to be thrown.

What is meant by Invalid class exception?

As the name of the exception indicates that, the class of the object which is serialized or deserialized becomes invalid due to one of the  reasons which i have listed out before.This causes the class to be
invalid and the objects of which cannot be serialized or deserialized.This class extends the ObjectStreamException.


When this InvalidClassException:no valid constructor is thrown?

 This type of exception is thrown when inheritance is involved in the program.When inheritance is involved, the serialization process proceeds by serializing the objects of  child classes first and then moves up the hierarchy until the non-serializable parent class is reached.

When the objects are to be deserialized it starts from the non-serializable  parent class and moves down the hierarchy.Since the parent class is non-serializable the state information about the members of the  parent class can only be retrieved from the default constructor as it cannot be retrieved from the stream.Since this state information is available only in the default constructor the absence of which makes the class invalid.

Example of the InvalidClassException:

 import java.io.*;
import java.util.*;
class Parent                      //non-serializable parent class
{
String parentname;          
                                    //absence of no-arg constructor
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");
try
        {

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


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

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

Output:

java.io.InvalidClassException: Child1; Child1; no valid constructor
        at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at Child2.main(Child2.java:34)


In the above program i have implemented a parent class Parent which is extended by a child class Child1. As i have told before the objects of class Child1 are serialized. When de-serializing it searches for default constructor in the parent class Parent as it is not present, this exception will be thrown.

Solution:

Include the no-arg constructor in the Parent class as follows:

Parent()
{
}




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