Carnal Carnal - 4 months ago 15
Java Question

Persistent java.lang.NullPointerException error from String.isEmpty()

I've been trying to search for the bug, but I couldn't find it. Already spent an hour trying to resolve what's wrong. The error begins when the code enters the isPlayerSet method

while (!player.isPlayerSet()) {
. I already set the used properties to "" but I am still getting this nullpointerexeption error. Please understand that I am fairly new in programming, especially in Java.

Here's the main class

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
}
}


And here's the player class

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;
}
}


I also have another question, is it advisable to replace
players[counter]
with
Player player
?

Answer

You are creating new Player objects here:

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

But: you are not storing those players in the array that you defined above. Thus: the array elements stay at its initial value - meaning that all players in the player array ... are null.

So your loop should say

player[i] = new Player();

And of course, you really want to read this here.