jadeeyedgirl
2009-02-25 14:27:09 UTC
The components of the maze is a 2D array of type Cell, which I created a child class that implemented JLabel as well as icons to represent open space, walls, the player, etc. The array I'm having issue with is mazeGrid which is of type Cell. But I don't believe that much else is relevant to my issue.
When I fill the array with icons (to start, I used spaces to represent blank area), it works fine. When I change one of the icons to represent a player, it works fine. My key handler works, and reads when I use the arrow keys.
My issue comes when I put a private method reference in the key listener. The method is being read, until I add something to reference my array. Here is my method that is being referenced:
private void movePlayerUp()
{
if (playerAtX != 0) {
mazeGrid[playerAtX][playerAtY]. setIcon(Cell.BLANK);
mazeGrid[playerAtX - 1][playerAtY].setIcon(Cell.PLAYER);
playerAtX = playerAtX - 1;
System.out.println(playerAtX);
}
}
Before you ask, all variables have been instantiated in the constructor and used in other places correctly through the code, in different methods. The PLAYER and BLANK reference the icons used in the Cell class and both work. The playerAtX and playerAtY reference the current place where the player is.
Now, whenever I try and run this code I get a null pointer exception, specifially at the line where I try and set the icon of mazeGrid[x][y].
What gets me is that this EXACT line of code is used in a different method and it works with no issues.
Does anyone have any ideas why this doesn't work? I've tried debugging and rewriting with no avail. Even using regular integers instead of variables doesn't work.