Results 1 to 2 of 2
  1. #1
    Herah is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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();
    There are quite a few things that could be null:

    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 09:55 AM.

Similar Threads

  1. Null pointer exception
    By jessie in forum New To Java
    Replies: 5
    Last Post: 02-08-2011, 03:58 PM
  2. Null Pointer Exception
    By musasabi in forum New To Java
    Replies: 3
    Last Post: 05-12-2010, 04:52 AM
  3. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 05:41 PM
  4. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 04:22 AM
  5. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •