2012-01-31 44 views
0

为什么我得到的分配的NPE:为什么在二维阵列中使用NPE?

mPyramid[row][column] = temp; 

这里是我的代码:

Block temp; 
    Block[][] pyramid = new Block[mInput.length][]; 

    for (int i = 0; i < pyramid.length; i++) { 
     pyramid[i] = new Block[mInput[i].length]; 
     for (int j = 0; j < pyramid[i].length; j++) { 
      pyramid[i][j] = new Block(); 
     } 
    } 

    for (int row = 1; row < mInput.length; row++) { 
     for (int column = 0; column < mInput[row].length; column++) { 
      temp = new Block(mInput[row][column], null, null); 
      mPyramid[row][column] = temp; 

      setParents(row, column); 
      temp.setPathNode(calculateDistance(temp)); 

     } 
    } 
} 
+1

我没有看到任何'mPyramid'声明。 – anubhava 2012-01-31 20:16:46

回答

3

似乎正在创建的局部变量pyramid然后引用mPyramid

+0

谢谢,只需要第二双眼睛就可以看到。将被接受的答案。 – Justin 2012-01-31 20:16:55

0

因为mPyramid未初始化或子阵列mPyramid[row]未初始化。

我想你是在第二种情况。要立即使用高度和宽度初始化m金字塔,您可以使用:

mPyramid = new Block[height][width]; // Swap height and width if you prefer