user962206 user962206 - 4 months ago 10
Java Question

Deserializing an ArrayList. no valid constructor

This how I deserialize my arrayList which contains objects of identification

public void deserializeArrayList(){
String path = "./qbank/IdentificationHARD.quiz";
try{
FileInputStream fileIn = new FileInputStream(path);
ObjectInputStream in = new ObjectInputStream(fileIn);
ArrayList<Identification> list = (ArrayList<Identification>) in.readObject();
System.out.println(list);
}catch(Exception e){
e.printStackTrace();
}
}


This is how I serialize it

public void saveItemIdentification(ArrayList<Identification> identification,File file){
try{
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
out.writeObject(identification);
}catch(Exception e){
e.printStackTrace();
}
}


But when I deserialize it it gives me this errors

java.io.InvalidClassException: quizmaker.management.Identification; quizmaker.management.Identification; 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 java.util.ArrayList.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at quizmaker.management.Manage.deserializeArrayList(Manage.java:92)


This is line 92

ArrayList<Identification> list = (ArrayList<Identification>) in.readObject();


Why is this happening?

This is the code of
Identification
Object.

package quizmaker.management;
import java.io.Serializable;
import quizmaker.Accounts.Rights.IAnswerable;

public class Identification extends Question implements Serializable{

private static final long serialVersionUID = 2L;
private String question;
private String answer;

public Identification(String q , String a){
super(q,a);
}

public String toString(){
return String.format("Question: %s\n Answer %s", getQuestion(),getAnswer());
}
}

Answer

The problem is --> in java

Java serialization process  only continues in object hierarchy till the class
is Serializable i.e. implements Serializable interface in Java.

And in your class you are calling super class constructor which is not implements Serializable.

So that was the problem.. :)

For your Second question take a look JavaDoc

During deserialization, the fields of non-serializable classes will be 
initialized using the public or protected no-arg constructor of the class.
A no-arg constructor must be accessible to the subclass that is serializable.
The fields of serializable subclasses will be restored from the stream.