gpt4 book ai didi

java - 带数组的 if 语句的空指针异常

转载 作者:行者123 更新时间:2023-12-01 18:13:49 25 4
gpt4 key购买 nike

我在星号包围的行处不断收到空指针异常。我怎样才能解决这个问题?我不明白为什么会发生这种情况,因为我用 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];

关于java - 带数组的 if 语句的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30954531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com