Null Pointer Exception on a 2D array (Java)

Posted by user315156 on Stack Overflow See other posts from Stack Overflow or by user315156
Published on 2010-04-13T06:29:25Z Indexed on 2010/04/13 6:33 UTC
Read the original article Hit count: 483

Filed under:
|
|
|
|

I have a class, "Tetris", in which one of the instance variables is "board". "board" is 2D array of Color objects. Upon the creation of a tetris object I call a method that sets the dimensions of board and then sets all of the Color objects to be the Default value, that is to say, Color.blue.

public Tetris(int rows, int cols){ this.rows = rows; this.cols = cols; reset(rows, cols);

}

public void reset(int rows, int cols){ Color[][] board = new Color[rows][cols]; for(int i = 0; i

Unfortunately, when I run the code (which obviously has not been posted in its entirety) I get a null pointer exception on the line:

board[i][j] = DEFAULT_COLOR; // Color.blue; //DEFAULT-COLOR.

Is there anything obviously wrong with what I am doing? (Sorry if there are glaring format issues, this is my first time on Stack Overflow)

© Stack Overflow or respective owner

Related posts about java

Related posts about null