My code:
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);
}
}
Using this code throws a NullPointerException at grid.add(new int[n]). How can I add data to grid?