Add to Google Reader or Homepage 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, 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.util.*;
class Parent                      //non-serializable parent class
String parentname;          
                                    //absence of no-arg constructor
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");

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

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

        catch (Exception e)

Output: Child1; Child1; no valid constructor
        at Source)
        at Source)
        at Source)
        at Source)
        at Child2.main(

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.


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



Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Alberto P E said...

Hello, why you need to extend the parent class if you can modify it? I'm looking for a wrapper method to Serialize some nonserialize third party class.

Post a Comment

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