0

I am trying to duplicate an object with no success. I tried

  1. Serialization
  2. 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.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Shew
  • 1,557
  • 1
  • 21
  • 36

2 Answers2

1

Override the clone method in B:

public class B {
    private int f1;
    private String f2;

    @Override
    public B clone() {
        B clone = new B();
        clone.f1 = this.f1;
        clone.f2 = this.f2;
        return clone;
    }
}

And use it to create duplicate:

B b1 = new B();
//...
B b2 = b1.clone();

Update:

public B clone()  {
        B N = new B();
        try {
                N = (B)super.clone();
        } catch (CloneNotSupportedException e) {
              e.printStackTrace();
        }
        N.setBoard(this.getBoard());
        return N;
}

Try the following (assuming that B implements Cloneable):

@Override
public B clone() throws CloneNotSupportedException {
    return (B) super.clone();
}

Update:

Custom implementation for board:

class B implements Cloneable {
    private char[][] board;   

    @Override
    public B clone() throws CloneNotSupportedException {
        B clone = (B) super.clone();
        clone.board = new char[this.board.length][];
        for(int i=0; i<this.board.length; i++) {
            clone.board[i] = new char[this.board[i].length];
            System.arraycopy(this.board[i], 0, clone.board[i], 0, this.board[i].length);
        }
        return clone;        
    }

    public char[][] getBoard() {
        return board;
    }

    public void setBoard(char[][] board) {
        this.board = board;
    }

    @Override
    public String toString() {
        return Arrays.deepToString(this.board);
    }
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • clone() should never use new B(). this will break if B is subclassed, use B clone = (B) super.clone(); – MeBigFatGuy Nov 28 '11 at 00:07
  • @MeBigFatGuy: I am confused here. We are creating a duplicate of `B`. Now if we do `B clone = (B) super.clone();`, how do `super.clone()` return an instance of `B`. The super class (`Object` in this case) don't even know about `B`. Can you explain please? – Bhesh Gurung Nov 28 '11 at 01:50
0

Over the time I have found out that using a third party library to do object cloning can be a life saver. One of the projects I'd recommend is "Cloning", available at http://code.google.com/p/cloning/

Here's an example:

Cloner cloner=new Cloner();

MyClass clone=cloner.deepClone(o);
// clone is a deep-clone of o
Rafael Steil
  • 4,538
  • 4
  • 25
  • 30