我的代码:获得NullPointerException异常,同时增加元素ArrayList的
import java.util.Random;
import java.util.ArrayList;
public class Percolation {
ArrayList<int[]> grid;
Random dice = new Random();
public Percolation(int n){
for(int i=0;i<n;i++){
grid.add(new int[n]);
}
output(grid,n);
}
public void output(ArrayList<int[]> x,int n){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
System.out.println(x.get(i)[j]);
}
public static void main(String[] args){
Percolation p = new Percolation(2);
}
}
使用此代码抛出一个NullPointerException
在grid.add(new int[n])
。我如何将数据添加到grid
?
This works。谢谢 –