-2

I have three class as boat,car,truck etc who extend from vehicle , I write all the object of this in a file say vehicleOrder.dat like this :

fout = new FileOutputStream("VehicleOrders.dat");   
oos = new ObjectOutputStream(fout); 
oos.writeObject(v); //where v is object of boat,truck car etc

Till here its good ,it writes, but when I try to read the dat file like this,

fin = new FileInputStream("VehicleOrders.dat");
ois = new ObjectInputStream(fin);
vehicle readInstance=null;

do{
    readInstance = (vehicle)ois.readObject();
    if(readInstance != null)
    {
        orderList.add(readInstance);
    } 
}       
while (readInstance != null); 

it reads the two objects which are in the dat file but after that goes to the do again and gives null pointer exception

Snedden27
  • 1,870
  • 7
  • 32
  • 60
  • 2
    See this question http://stackoverflow.com/questions/18689814/invalid-class-exception-no-valid-constructor – Cyrille Ka Oct 10 '13 at 01:21

2 Answers2

1

It seems like your class doesn't have 0-param-Constructor. Maybe this link will help you out about the explanation

Community
  • 1
  • 1
Agung Pratama
  • 3,666
  • 7
  • 36
  • 77
0

ObjectInputStream expects a visible no-arg constructor for the class instance you are trying to deserialize. I assume your class doesn't have one. Add one.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724