0

Im trying to read and write an object to a .ser file. This object is composede by and array of another objects, and an integer. Like this:

public class  Ranking implements Serializable{
    private static ArrayList<Acabada> partides = new ArrayList<Acabada>();
    private int tipus_ordenacio;
}

When using serialitzation, the code seems to work. When I open the file generated there appears some unredeable stuff. But when writting (deserialization) I get this message:

tipus ordenacioo 1java.io.InvalidClassException: domain.Ranking; local class incompatible: stream classdesc serialVersionUID = 5620111485391998837, local class serialVersionUID = -3886134201979592842

This is the code I use to deserialize.

public void readFromFile() {


        try {
            FileInputStream fis = new FileInputStream("ranking.ser");

            ObjectInputStream ois = new ObjectInputStream(fis);
            domain.Ranking ranking = (domain.Ranking) ois.readObject();
           
            ois.close();
            fis.close();

   }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }

What am I doing wrong? Thanks!

sergi martinez
  • 165
  • 1
  • 8

1 Answers1

0

You should declare a serialVersionUID

Check out this answer What is a serialVersionUID and why should I use it?

Juli3n
  • 221
  • 1
  • 5