【发布时间】:2018-01-21 16:21:37
【问题描述】:
我想用 LinkedLists 创建一个矩阵并用 0 填充所有单元格。
private LinkedList<LinkedList<T>> matrix;
private int rows;
private int columns;
// constructor
public Matrix(int rows, int columns){
this.rows = rows;
this.columns = columns;
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
matrix.get(i).add(0); // here I get the NullPointerException
}
}
}
我做错了什么?
【问题讨论】:
-
data来自哪里? -
修复了它。应该是
matrix -
matrix.get(i)是null,改变这个事实。 -
@AniketSahrawat 如果 OP 这样做,整个 for 循环将不会做任何事情。
-
@luk2302 我不明白,什么意思?
标签: java class constructor nullpointerexception