I want to seriliaze an object with this method :
public void serializ(CRDT m) throws IOException {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(byteOutput);
stream.writeObject(m);
sumMemory = byteOutput.size();
stream.flush();
stream.close();
byteOutput.flush();
byteOutput.close();
}
I have an exception java.lang.StackOverflowError
Exception in thread "main" java.lang.StackOverflowError
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1169)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
...
I read some forum that i need to reimplementing the writeObject / readObject methods.
Is it the only possible solution ? and how to reimplementing the writeObject / readObject
My object to serialize is :
http://pastebin.com/D1kEidtn
Two class that cause errors are :
pastebin.com/Sb3X0Quq and enter link description here
CRDT is the superclass of the Object m which is serialized.
The error is that the class that derives from CRDT seems to have a reference to itself, leading to an endless recursion
You see that in the stack trace.
Tipp find out the class of The object CRDT m: by either using the debugger, or adding an System.out.println(m.getClass()) at the beginn of your serialize() method.
Then when you know the class, check wheter the object has a reference to itself.