Search code examples
javaarraydeque

2d Arraydeque insertion


I am using arraydeque to store another arraydeque of type Integer. My code looks like this:

    private ArrayDeque<ArrayDeque<Integer> > grid;
    public void initiateGrid(){     
    for (int i = 0; i < Length; i++)
    {
        ArrayDeque<Integer> columns = new ArrayDeque<Integer>();
        for (int j = 0; j < Width; j++)
            columns.add((int)(Math.random() * 100));
        grid.add(columns);          
    }
}   

When I try to run this it gives me a NullPointerException on grid.add(columns). does anyone know what am I doing wrong? Also if there is a better way to do this, please let me know.

Thanks


Solution

  • initialize grid, because you cannot use a null object, otherwise you get NullPointerException

     public void initiateGrid(){ 
        grid = new ArrayDeque<ArrayDeque<Integer>>();
        //...
     }