I'm trying to copy an Object I've created from one array to another of the same type in Java. When I run my program I receive a NullPointerException.
The relevant class functions are:
private int mState;
public Cell(int pRow, int pColumn, int pState) {
//other things
setState(pState);
}
public void setState(int pNewState) {
mState = pNewState;
}
public void setDead() {
mState = DEAD;
}
and the line in which the error occurs:
mFutureGeneration[i][j].setDead();
That array is defined as
private Cell [][] mFutureGeneration;
then dimensioned as
mFutureGeneration = new Cell[100][100];
It receives its contents from:
Cell [][] vSeedArray = new Cell[100][100];
which is filled as
for (int i = 0; i<100; i++) {
for (int j = 0; j<100; j++) {
int vNewState = mGenerator.nextInt(2) - 1;
vSeedArray[i][j] = new Cell(i,j,vNewState);
}
}
I think the problem is happening in the copy, but I was always under the impression Java copied by reference, so I can't see why it would be failing.
I copy the contents across with a loop
for(int i = 0; i<vSeedArray.length; i++) {
for(int j=0; j<vSeedArray[i].length; j++) {
mCurrentGeneration[i][j] = vSeedArray[i][j];
}
}
Any help would be appreciated.
Thanks.
i,j, andmFutureGeneration[i][j]at the line withmFutureGeneration[i][j].setDead();.