Results 1 to 5 of 5
  1. #1
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Bingo Null Pointer Exception

    So I finished up my BingoCard class last week, to be given another assignment on adding a cover and hasBingo method. I have to compare strings using the equals method, so it knows when to cover up squares that have a match. I wrote all my code, but keep getting a null pointer when i try running it
    Java Code:
    public class BingoCard
    {
    
     private BingoSquare[][] myBingoCard = new BingoSquare[5][5];
     
     public BingoCard()
     {
      
      for (int row = 0; row < myBingoCard.length; row++) {
       for (int col = 0; col < myBingoCard[row].length; col++){
        myBingoCard[row][0] = (BingoSquare) new BingoSquare (3 * row + 3);
        myBingoCard[row][1] = (BingoSquare) new BingoSquare (3 * row + 18);
        myBingoCard[row][2] = (BingoSquare) new BingoSquare (3 * row + 33);
        myBingoCard[row][3] = (BingoSquare) new BingoSquare (3 * row + 48);
        myBingoCard[row][4] = (BingoSquare) new BingoSquare (3 * row + 63);
        myBingoCard[2][2] = null;
       } 
      }
      
     }
     
     public String toString()
     {
      String newCard = "";
      
      for (int row = 0; row < myBingoCard.length; row++) {
       for(int col = 0; col < myBingoCard[row].length; col++) {
        if (myBingoCard[row][col] != null) {
         newCard += " " + myBingoCard[row][col];
        }
        else {
         newCard += " ****";
        }
       }
       
       
       newCard += "\n";
      }
      
      return newCard;
         
     }
    
    
    public boolean cover(BingoBall bingoBall) 
    {
    	
    	   for (int row  = 0; row < myBingoCard.length; row++) 
    	   {
    	     for (int col = 0; col < myBingoCard[row].length; col++) 
    	     {
    	       if (myBingoCard[row][col].equals(bingoBall.toString())) {
    	         myBingoCard[row][col] = null;
    	    	 return true; 
    	      }
    	     }
           }
    	
    	   return false;
     
    }
    
    public boolean hasBingo() 
    {
    	
    	for (int row = 0; row < myBingoCard.length; row++)
    	{
    		for (int col = 0; col < myBingoCard[row].length; col++)
    		{
    			if (myBingoCard[row][col] == null) {
    				return true;
    			}
    		}
    	}
    	
    	return false;
     }
    
    
    }
    output:
    Exception in thread "main" java.lang.NullPointerException
    at BingoCard.cover(BingoCard.java:52)
    at BingoGame.main(BingoGame.java:17)

    BingoGame class:
    Java Code:
    public class BingoGame
    {
      /******************************************************************
        Creates myBingoCard as a 5 x 5 array of BingoBalls
        Creates 5 specific BingoBalls and covers myBingocard
        Prints "Bingo" on a true return from cover method
        Prints final state of myBingoCard
      *******************************************************************/
      public static void main (String[] args)
      {
        // create my Bingo Card
        BingoCard myBingoCard = new BingoCard();
    
        // create some Bingo Balls, cover on myBingoCard, and check for Bingo
        // change BingoBall values to test numbers on and not on your card
        // test at least one Bingo on a row, a column, or a diagonal
        if (myBingoCard.cover(new BingoBall(1)) && myBingoCard.hasBingo())  // not on myBingoCard
          System.out.println("Bingo on 1st");    
        if (myBingoCard.cover(new BingoBall(3)) && myBingoCard.hasBingo())  // next four are a diagonal
          System.out.println("Bingo on 2nd");
        if (myBingoCard.cover(new BingoBall(21)) && myBingoCard.hasBingo())
          System.out.println("Bingo on 3rd");
        if (myBingoCard.cover(new BingoBall(57)) && myBingoCard.hasBingo())
          System.out.println("Bingo on 4th");
        if (myBingoCard.cover(new BingoBall(75)) && myBingoCard.hasBingo())
          System.out.println("Bingo on 5th");
        
        // print the final state of myBingoCard
        System.out.println(myBingoCard);
      }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Bingo Null Pointer Exception

    Note the BingoCard constructor sets a square (at [2][2]) to null, and you cannot use equals on null (see the cover method). Those line numbers in the exception should help you locate exactly where the exception is being thrown, helping you backtrack through the code. A debugging tip is to add some println's in there to help check the values

  3. #3
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Bingo Null Pointer Exception

    Well the exception occurs on the line where I am using the equals method to compare the two strings. As for the square at [2][2], I needed to set that to null so that my toString method would work correctly and give me that free space in the middle of the bingo card. I managed to get rid of the exception by moving the myBingoCard[2][2] = null line down to the toString method. Im still having trouble seeing how to get the two strings to compare, and correctly print out the stars in the squares that satisfy the condition.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Bingo Null Pointer Exception

    Java Code:
    if (something != null && something.equals(somethingElse)) { ... }
    db

  5. #5
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Bingo Null Pointer Exception

    Sigh, I always make stupid mistakes like not adding the boolean check beforehand. Thanks for the help.

Similar Threads

  1. Null Pointer Exception HELP!?
    By 2wyked in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 01:41 AM
  2. Null Pointer exception (Again !!)
    By mobosecomin in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 05:04 PM
  3. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 02:25 PM
  4. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  5. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 04:17 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
  •