java - Getting NullPointerException while adding element to ArrayList -
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 code throws nullpointerexception
@ grid.add(new int[n])
. how can add data grid
?
you haven't initialize arraylist
.
arraylist<int[]> grid = new arraylist<>();
Comments
Post a Comment