Question:
Null Pointer Exception while using a 2D array?
jadeeyedgirl
2009-02-25 14:27:09 UTC
I have a project that I'm currently doing for my computer programming class. The project is to make a 2D maze using java and I'm having an issue with getting a null pointer exception that I can't seem to debug.

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.
Three answers:
owsley's kid
2009-02-25 14:34:59 UTC
You're not checking to make sure that PlayerX -1 is inside your array. The null pointer exception makes me think you're outside of it.

This may not have anything to do with your problem, just readability for someone trying else trying to help you debug it, but in two dimensions, X is usually the horizontal axis(left or right) and Y is vertical ( up or down). Without the method name, I'd assume that this function moves one to the left.
Pat M
2009-02-25 14:42:42 UTC
The problem is not here but where it's being called from. That null pointer exception means that mazeGrid[playerAtX][playerAtY] is null. Can't tell from that code fragment why but that is the problem.



EDIT: The value is null. If it worked in previous lines of code but not now, you must have changed it somewhere in between. If you want proof add the line System.out.println(mazeGrid[x][y]); just before before the exception.



The only reason you can get a null pointer exception is when you "Thrown when an application attempts to use null in a case where an object is required. These include: ... Calling the instance method of a null object... Accessing or modifying the slots of null as if it were an array." These are the only 2 things you do on that line that could throw a null pointer exception. So either mazeGrid is null or mazeGrid[x][y] is null.
reyna
2016-05-29 09:55:32 UTC
Let's say that you want an array H elements tall and W elements wide, and that you have a pointer P. Assuming you have subscripts x (in 0 to W-1) and y (in 0 to H-1), you can use either P[y*w+x] or P[x*h+y]. The former is most common, and you must be consistent in your usage. Note also that P[n] is the same as *(P+n).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...