Results 1 to 2 of 2
Thread: null pointer exception
- 12-01-2011, 08:27 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 10
- Rep Power
- 0
null pointer exception
I cannot for the life of me figure out why I am getting a null pointer exception here.
Java Code:public void boardBuilder(){ String[][] gameBoard = new String[Variables.getBoardSize()][Variables.getBoardSize()]; // board size is returned by this call as 3, so this is a 3 by 3 array int buttonLocator = 0; for(int i = 0; i < Variables.getBoardSize(); i++){ // row for(int k = 0; k < Variables.getBoardSize(); k++){ // column gameBoard[i][k] = Variables.getButtons()[buttonLocator].getText(); // error is here. I am wanting to get the text of a JButton in a 1D array and set it equal to a location in the 2D array buttonLocator++; } // end inner for } // end outer for checkBoard(gameBoard); boardDismantler(gameBoard); } // end boardBuilder
- 12-01-2011, 08:44 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: null pointer exception
I cannot for the life of me figure out why I am getting a null pointer exception here.Java Code:gameBoard[i][k] = Variables.getButtons()[buttonLocator].getText();
gameBoard
gameBoard[i]
gameBoard[i][k]
Variables.getButtons()
Variables.getButtons()[buttonLocator]
(Basically the NPE arises because you cannot "dereference" with [] or . something that has the value null.)
Use System.out.println() to display the value of each just before you get the NullPointerException.
-----
I'm assuming Variables is a class, otherwise you would have to check if it was null. Using static methods and variables is just asking for NPE and other troubles because anywhere in the code can clobber a perfectly good value with null thereby making looking for the *cause* of the NPE rather hard work.
[Edit] On second thoughts, it is static variables that are particularly nasty in this regard. But still, things shouldn't be static without some good reason.Last edited by pbrockway2; 12-01-2011 at 08:55 AM.
Similar Threads
-
Null pointer exception
By jessie in forum New To JavaReplies: 5Last Post: 02-08-2011, 02:58 PM -
Null Pointer Exception
By musasabi in forum New To JavaReplies: 3Last Post: 05-12-2010, 03:52 AM -
Help with Null Pointer Exception
By Beginner in forum New To JavaReplies: 2Last Post: 04-17-2010, 04:41 PM -
Null pointer exception?
By coffee in forum New To JavaReplies: 4Last Post: 08-03-2009, 03:22 AM -
null pointer exception
By jyothi.priyanka in forum New To JavaReplies: 12Last Post: 03-11-2009, 05:04 PM
Bookmarks