I have a class called Player that defines the characteristics of a Player. I have a method called readPlayer in another class:
void readPlayer(String playerInfo) {
int j = 0;
int playerNumber = 0;
int playerCount = 0;
String splitPipe = "\\|"; // split by pipe
String splitColon = "\\:"; // split by colon
String splitInput[] = playerInfo.split(splitPipe + "|" + splitColon);
for (int i = 0; i < splitInput.length; i += 5) {
playerCount++; // keeps track of how many players there are
}
players = new Player[playerCount];// initializes array to amount of players
while (playerNumber < playerCount) {
players[playerNumber] = new Player(Integer.parseInt(splitInput[j]), splitInput[j + 1], splitInput[j + 2],
splitInput[j + 3], splitInput[j + 4]);
j += 5;
playerNumber++;
}
pR.printPlayers(players); //ERROR: NullPointerException thrown here
}
But when I try to pass the players array to printPlayers in another class it gives me and NullPointerException error
Details of error:
Exception in thread "main" java.lang.NullPointerException
at ca.bcit.comp2613.PlayerReader.readPlayer(PlayerReader.java:30)
at ca.bcit.comp2613.Lab2.main(Lab2.java:16)
This is my main method:
static PlayerReader p = new PlayerReader();
public static void main(String[] args) {
if(args.length == 0) {
System.out.println("Usage java -jar project.jar <input String>");
System.exit(-1);
} else {
p.readPlayer(args[0]);
}
}
}
This is my printPlayer method - Nothing special not doing anything with it right now
public class PlayerReport {
public void printPlayers(Player players[]) {
}
}