I'm creating a simple game, and was trying to clone the objects that makeup the board to do AI processing without effecting the actual game.

I'm getting "Unhandled Exception: java.lang.CloneNotSupportedException" even though everywhere in the stack/where I'm using these clones I have in try/catch loops.

Here is the code. I have my Space class (most of it is snpied) which implements Clonable (might be part of the problem):

public class Space extends Pane implements Cloneable {



    public Space(int x, int y, boolean light) {
    }

    protected Object clone() throws CloneNotSupportedException {
        try {
            return super.clone();
        } catch (Exception e) {}

        return new Object();
    }
}

In another (object) class I attempt to make a copy of this array of Space objects:

    public Space[][] copyBoard(Space[][] thisBoard) throws CloneNotSupportedException {
        Space[][] copy = new Space[8][8];
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                Space thisSpace = thisBoard[x][y];
                try {
                    copy[x][y] = (Space) thisSpace.clone();
                } catch (Exception e) {}
            }
        }
        return copy;
    }

Lastly, where the error is in another class (object):

 try { //this in my constructor
     space.setOnMouseClicked(e-> onClick(finalX,finalY)); //error on "onClicked"
 } catch (Exception e) { }



 public void onClick(int x, int y) throws CloneNotSupportedException{
 if (processMove(move)) {
    setActiveSpace(null);
    try {
        processMove(compPlayer.getBestMove(spaceBoard)); //getBestMoves copys SpaceBoard, and deals w/ the exception too.
    } catch (Exception e) {}
}    

"onClick" is underlined red saying the unhandled clone error.

If you need more of my code let me know, I cut it out to shorten things up (although there may be refrences to some missing variables or things).

Thanks

0 Answers