NullPointerException returning an array that will filled in a different method
<p>I am getting a NullPointerException when the getCave method is called when it tries to return the 2D array. I have not been able to find a solution online. I can get the program to run without the exception by replacing the return with a new Cave that is not an array but that would not fit my needs. Here is a simplified version of my code:</p>

<pre><code>import java.util.Random;


public class Board {

    public static final int DEFAULT_ROWS = 10;
    public static final int DEFAULT_COLS = 10;


    Cave[][] caveArray = new Cave[DEFAULT_ROWS+2][DEFAULT_COLS+2];


    public Board(int rows, int cols){

        Random rand = new Random();


                for (int j = 1; j &lt; (cols+1); j++) {

                    for (int i = 1; i &lt; (rows+1); i++) {

                        Cave temp;
                        temp = new Cave(i, j);

                        int rnum = rand.nextInt(100)+1;


                        if (rnum &gt; 50) {
                                caveArray[i][j]=temp;
                                caveArray[i][j].makeBlocked(); 
                        }


                        else if(rnum &lt;=50) {
                                caveArray[i][j]=temp;
                                caveArray[i][j].makeOpen();
                        }
                    }
                }           
    }

    public Cave getCave(int r, int c){

        return caveArray[r][c];
    }

}
</code></pre>

<p>here is the caller:</p>

<pre><code>private void newGame() {
    // Set up the game board.
    gameBoard = new Board(DEFAULT_ROWS, DEFAULT_COLS);

    // Set up the 3 characters.
    characters = new ArrayList&lt;Character&gt;();

    // Add the adventurer (always in the top left).
    characters.add(new Adventurer(gameBoard.getCave(0, 0)));
    selected = 0; // Initially select the adventurer.
}
</code></pre>

<p>which calls:</p>

<p>public class Adventurer extends Character{</p>

<pre><code>Adventurer(Cave initLoc) {
    super(initLoc);


}
</code></pre>

<p>which calls:</p>

<pre><code>public abstract class Character implements CaveWorker{

protected Cave location;

public Character(Cave initLoc){
    location = initLoc;

    location.setOccupied(true);
}
</code></pre>
