来自String.isEmpty()的持久性java.lang.NullPointerException错误

时间:2016-07-17 07:05:28

标签: java string oop nullpointerexception

我一直在尝试搜索错误,但我找不到它。已经花了一个小时试图解决什么是错的。当代码进入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吗?

4 个答案:

答案 0 :(得分:3)

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

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

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

所以你的循环应该说

players[counter] = new Player();

当然,你真的想读这个here

答案 1 :(得分:2)

你在这个循环中破坏了同一个变量。

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

选项1:

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

选项2(稍微简洁优雅):

for (Player p: players) p = new Player();

答案 2 :(得分:2)

在循环中

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

您初始化局部变量player,因此while (!player.isPlayerSet()) player中的nullplayers。您需要在for (int counter = 0; counter < players.length; counter++) { players[counter] = new Player(); } 数组

中初始化实例
node-telegram-bot-api

答案 3 :(得分:1)

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

应该是这个。

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