-1

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[]) {

    }
}

2 Answers2

0
    public class Player {
    public Player(int parseInt, String string, String string2, String string3,
            String string4) {
        // Do initialization
    }

    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]);
        // }
        PlayerReader p = new PlayerReader();
        p.readPlayer("playerInfo");
    }
}

class PlayerReport {

    public void printPlayers(Player players[]) {
        System.out.println("Printing....");
    }
}

class PlayerReader {

    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
        }
        Player[] 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]);
            players[playerNumber] = new Player(1, "3", "4", "5", "6");

            j += 5;
            playerNumber++;
        }
        PlayerReport pR = new PlayerReport();
        pR.printPlayers(players); // ERROR: NullPointerException thrown here
    }
}
chinna_82
  • 6,353
  • 17
  • 79
  • 134
0

As alluded to in another answer, you haven't shown how you have instantiated either of the "pR" or "players" objects.

Not sure what the "pR" object is but that is what is causing your Null Pointer.

Scott Bennett-McLeish
  • 9,187
  • 11
  • 41
  • 47