初始化onSizeCanged()中的对象时的NPE

时间:2014-08-06 18:03:04

标签: java android

我有一个名为Table的类,我声明了这个类的一个对象,在onSizeChanged()我初始化它,但是当我尝试在onDraw()内引用它时,应用程序崩溃并抛出NPE

代码

public class GameView extends View {
private Table mTable;
....
....
protected void onDraw(Canvas canvas) {

canvas.drawText("Table_Height: "+mTable.getTableHeight(), 10, 15*  
(cardArraySize_Height), textPaint);
....
....
....
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
setupMainTable(mTable);
...
...
...
}

private void setupMainTable(Table table) {

table = new Table(10,
  ((screenHeight/2)-(2*cardHeight)),
  (screenWidth-10),
  ((screenHeight/2)+(2*cardHeight)));

}

1 个答案:

答案 0 :(得分:2)

Java按值传递而不是通过引用传递。所以你需要这样做: -

private void setupMainTable() {

this.mTable= new Table(10,
  ((screenHeight/2)-(2*cardHeight)),
  (screenWidth-10),
  ((screenHeight/2)+(2*cardHeight)));
}