samedi 20 juin 2015

Null Pointer Exception with if statement with Array

I keep getting a null pointer exception at the line surrounded by stars. How can I fix this? I don't understand why it's happening because I filled currentGuessArray with false. (I didn't include all of my code, just the parts that have to do with currentGuessArray.)

public static boolean [] currentGuessArray;


public void initGuess() { 
   // creates a new Array for the currentGuessArray variable 
   boolean [] currentGuessArray = new boolean[20];
   // initialize all slots to false
   Arrays.fill(currentGuessArray, false);
}

public String getCurrentGuessArray() { 
   // the returned String has "_ " for unrevealed letters
   // “walk over” the currentWord and currentGuessArray Arrays and create the String
   if ( !currentWord.equals("Shrek") && !currentWord.equals("Dobby") ) {
       int j = 1;
       while ( (!currentWord.substring(j, j).equals(" ")) && (j < currentWord.length()) ) {
          j++;
          spaceSpot = j;
       }
   }
   int k = 0;
   String displayString = "";
   while ( k < currentWord.length() ) {
      if ( k == spaceSpot ) {
         displayString = displayString + "   ";
      }
      **else if ( currentGuessArray[k] == true ) {**
         displayString = displayString + currentWord.substring(k, k);
      }
      else {
         displayString = displayString + "_ ";
      }
      k++;
   }
   return displayString;        
}

Aucun commentaire:

Enregistrer un commentaire