在星号包围的行中,我不断收到空指针异常。我怎样才能解决这个问题?我不明白为什么会这样,因为我用false填充了currentGuessArray。
(我没有包括我的所有代码,只是与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;
}
最佳答案
假设正在调用initGuess
,则您是shadowing currentGuessArray
。更换
boolean [] currentGuessArray = new boolean[20];
与
currentGuessArray = new boolean[20];