I am trying to duplicate an object with no success. I tried
- Serialization
- Cloning
both methods don't work for me.
When I used serialization (I am using the technique specified here Faster Deep Copies of Java Objects) I got NullPointerException
. With cloning I got original object reference.
Scenario is:
I have one abstract class A with data char[][] board
and an extended class B
. I want to duplicate the data board
for this I implemented two methods in B
- getboard()
and setboard()
and implemented a clone method such that
B b1 = new B;
B.initialize();
B b2 new B;
B2 = B1.clone(B2)
But this is not working. Any help would be appreciated.
Thanks :-)
public B clone() {
B N = new B();
try {
N = (B)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
N.setBoard(this.getBoard());
return N;
}
Regarding the serialization, after deserializing when i try to draw the board it is giving me NullPointerException
. I conclude that, deserialization didn't work properly.