我的代碼:獲得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。謝謝 –