Dániel Barta
Dániel Barta

Reputation: 1062

readObject() is not working and breaks from method

I'd like to read different kinds of objects from file to an ArrayList, all of them are instances of class which extend the class Advertisement. I'm trying with this code:

ArrayList <Advertisement> ads = new ArrayList<Advertisement>();
ObjectInput input2 = new ObjectInputStream(
    new BufferedInputStream(new FileInputStream("ads.ser")));

//break from this method at this point
ads = (ArrayList<Advertisement>) input2.readObject(); 

The problem is at the 3rd line/last line. It doesn't read the objects in variable ads of type ArrayList<Advertisement>, furthermore, it breaks from this method, without any messages.

Edit: Removed the try-catch block, I should have done it earlier, but I still don't know the solution. Stacktrace:

java.io.InvalidClassException: Kiado; local class incompatible: stream classdesc serialVersionUID = -1393576200767336208, local class serialVersionUID = -841663850423605586
    at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(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 Main.beolvasas(Main.java:30)
    at Main.startup(Main.java:57)
    at Main.main(Main.java:633)

Upvotes: 1

Views: 2374

Answers (2)

TofuBeer
TofuBeer

Reputation: 61526

Do you have any code like:

catch(<WheteverException> e)
{
   // nothing here
}

If so do the following:

  1. change the // nothing here to e.printStackTrace();
  2. remove the try/catch and only add it back if the compiler tells you to, and then only add back what exception that the compiler tells you to add (for instance don't do catch(Exception e).

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691655

Now that you have a stack trace, the problem is clear: since the moment you serialized the objects to the file, you have made changes to the class Kiado. So the class you have now is not compatible anymore with the class as it was when serializing the objects to the file.

If you didn't change the number and the names of the fields of the class (and of all its superclasses), you can make it compatible again by just adding the following variable declaration in the class:

private static final long serialVersionUID = -1393576200767336208;

If you added, removed or renamed at least one (non-transient) field, then you might make the new class still compatible with the old one, but with more pain. And we would have to know the nature of the changes to help you.

I would personally avoid using native serialization for long-term storage because, as you just noticed, it's fragile and makes it hard to change the model. I would use a more easily readable and migratable file format: XML or JSON for example.

Upvotes: 1

Related Questions