我在星星包围的行处不断收到空指针异常。我怎样才能解决这个问题?我不明白为什么会发生,因为我用false填充currentGuessArray。 (我没有包括所有的我的代码,只是有currentGuessArray做的部分。)带有数组的if语句的空指针异常
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;
}
currentGuessArray'数组是静态的任何具体原因? – Pshemo