Java - Deserialization InvalidClassException (No valid constructor)

后端 未结 3 1501
长发绾君心
长发绾君心 2021-02-19 02:33

I\'m trying to serialize an object and then deserialize it after sending its data to a client program.

Here\'s an example of how the object\'s inheritance works. The obj

3条回答
  •  无人共我
    2021-02-19 03:03

    Good explanation is done in answers for following question Deserializing an ArrayList. no valid constructor

    Long story short - you need no-arg constructor for first nonserializable super class of your class, NPC in your case.

    If you don't have an access to NPC and it doesn't contain no-arg constructor - then you can add one more 'fake' class to hierarchy which will choose the correct one. E.g.

    class SomeClass extends NPC {
    // will be called during deserialization
    public SomeClass(){
    // call custom constructor of NPC
    super(null);
    }
    }
    
    class Person extends SomeClass implements Serializable {
    // ..
    }
    

提交回复
热议问题