gpt4 book ai didi

java - String.isEmpty() 持续出现 java.lang.NullPointerException 错误

转载 作者:行者123 更新时间:2023-12-01 07:02:31 25 4
gpt4 key购买 nike

我一直在尝试寻找该错误,但没有找到。已经花了一个小时试图解决问题。当代码进入 isPlayerSet 方法 while (!player.isPlayerSet()) { 时,错误开始。我已经将使用的属性设置为“”,但仍然收到此 nullpointerexeption 错误。请理解,我在编程方面还很陌生,尤其是 Java。

这是主类

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String playerName = "";
int chosenPokemon = 0;
boolean isANumber = false;;
Player player;

/*
* Initialize Players
*/
Player[] players = new Player[2];

for (int counter = 0; counter < players.length; counter++) {
player = new Player();
}

/*
* Get details of trainers
*/
for (int counter = 0; counter <= players.length-1; counter++) {
player = players[counter];
while (!player.isPlayerSet()) {
/*
* Input player name
*/
if(player.getPlayerName() == "") {
System.out.println("Enter a valid name for Player " + (counter+1) + ":");
player.setPlayerName(playerName);
}
/*
* Choose Pokemon
*/
if(player.getChosenPokemon() == ""){
System.out.println("Choose a starting pokemon for Player " + (counter+1) + ":");
System.out.println("[1] Charmander");
System.out.println("[2] Bulbasaur");
System.out.println("[3] Squirtle");

do {
if(!scanner.hasNextInt())
{
System.out.println("Input must be a valid integer. Try Again.");
scanner.next();
}
else if(!(chosenPokemon >= 1) && !(chosenPokemon <= 3))
{
System.out.println("Input must be a number from 1-3. Try Again.");
scanner.next();
}
else {
chosenPokemon = scanner.nextInt();
isANumber = true;
}
} while(!isANumber);
player.setChosenPokemon(chosenPokemon);
}
} // End of while loop
} // End of for loop
}
}

这是玩家类别

public class Player {
Scanner scanner = new Scanner(System.in);

private String playerName = "";
private String chosenPokemon = "";

public String getPlayerName() {
return this.playerName;
}

public void setPlayerName(String playerName) {
do {
playerName = scanner.nextLine();
if(!isAlpha(playerName)) {
System.out.println("Invalid input. Try again");
}

if(playerName.isEmpty()) {
System.out.println("Player name cannot be blank! Try again");
}
} while(!isAlpha(playerName) || playerName.isEmpty());

this.playerName = playerName;
System.out.println("Welcome " + this.playerName);
}

public String getChosenPokemon() {
return chosenPokemon;
}

public void setChosenPokemon(int chosenPokemon) {
if(chosenPokemon == 1) {
this.chosenPokemon = "Charmander";
} else if(chosenPokemon == 2) {
this.chosenPokemon = "Bulbasaur";
} else {
this.chosenPokemon = "Squirtle";
}
}

public boolean isPlayerSet() {
if (this.playerName.isEmpty() && this.chosenPokemon.isEmpty()) {
return false;
}

return true;
}

public static boolean isAlpha(String name) {
char[] chars = name.toCharArray();

for (char c : chars) {
if (!Character.isLetter(c)) {
return false;
}
}

return true;
}
}

我还有一个问题,是否建议将 players[counter] 替换为 Player 玩家

最佳答案

您正在此处创建新的 Player 对象:

for (int counter = 0; counter < players.length; counter++) {
player = new Player();
}

但是:您没有将这些玩家存储在您上面定义的数组中。因此:数组元素保持其初始值 - 意味着玩家数组中的所有玩家...均为空。

所以你的循环应该说

players[counter] = new Player();

当然,你真的想读这篇文章here .

关于java - String.isEmpty() 持续出现 java.lang.NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38418750/

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