在Java中将对象从一个数组复制到另一个数组
我试图将我创建的对象从一个数组复制到Java中相同类型的另一个数组。当我运行我的程序时,我收到一个NullPointerException 相关的类功能包括:在Java中将对象从一个数组复制到另一个数组,java,Java,我试图将我创建的对象从一个数组复制到Java中相同类型的另一个数组。当我运行我的程序时,我收到一个NullPointerException 相关的类功能包括: private int mState; public Cell(int pRow, int pColumn, int pState) { //other things setState(pState); } public void setState(int pNewState) { mState = pNewSt
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;
}
以及发生错误的行:
mFutureGeneration[i][j].setDead();
该数组定义为
private Cell [][] mFutureGeneration;
然后标注为
mFutureGeneration = new Cell[100][100];
其内容来自:
Cell [][] vSeedArray = new Cell[100][100];
填写为
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);
}
}
for(int i=0;i我不确定您是否分配过任何“单元”对象—只分配用于容纳它们的数组
如果是这样的话,这可能就是导致NullPointer异常的原因
好链接:
向下滚动至“对象数组”部分
'希望这会有帮助!我不确定您是否分配过任何“单元”对象—只分配用于容纳它们的数组
如果是这样的话,这可能就是导致NullPointer异常的原因
好链接:
向下滚动至“对象数组”部分
'希望这有帮助!McCurrentGeneration是如何实现mFutureGeneration的?我建议您从使用调试器(或添加SOP语句)开始。检查I
、j
和mFutureGeneration[I][j]
的值,并将其与mFutureGeneration[I][j]对齐。setDead()
@EliAlgranti这就是问题所在。我有两个数组的mFutureGeneration和mCurrentGeneration。我一直在重构,从在数组中使用简单的0和1到拥有一个对象,但忘记了在mFutureGeneration数组中实际创建对象。谢谢!@xyzjace-没问题,有时候你只需要有人问你“你确定打开了吗?”:-)mCurrentGeneration如何进入mFutureGeneration?我建议你从使用调试器(或添加SOP语句)开始。检查I
、j
和mFutureGeneration[I][j]
在mFutureGeneration[I][j]行的值。setDead();
@EliAlgranti这就是问题所在。我有两个数组的mFutureGeneration和mCurrentGeneration。我一直在重构,从在数组中使用简单的0和1到拥有一个对象,但忘记了在mFutureGeneration数组中实际创建对象。谢谢!@xyzjace-没问题,有时候你只需要有人问你就可以了。”你确定你把它打开了吗?:-)
for(int i = 0; i<vSeedArray.length; i++) {
for(int j=0; j<vSeedArray[i].length; j++) {
mCurrentGeneration[i][j] = vSeedArray[i][j];
}
}