3

I'm trying to make a simple game in Java. In my main class, I let the player type in their names and how many players will be playing.

here is my code in the main class:

{...}
Board game = new Board (playerNames,numberOfPlayers);
        game.run();
{...}

Now in my other class it looks like this:

    private int numberOfPlayers;
    private Player[] players = new Player[numberOfPlayers];
    private String[] playerNames= new String[numberOfPlayers];
    private PlayerHand[] hands = new PlayerHand[numberOfPlayers];

public Board(String[] s, int n) {   
        playerNames=s;
        numberOfPlayers= n;
    }

it then initializes the Players like this:

public  void initializePlayer(){    
        for(int i=0;i<numberOfPlayers;i++){
            hands[i]=new PlayerHand();
            players[i]=new Player(playerNames[i],hands[i]);}

For testing I always set the numberOfPlayers directly in the Board class. Since I want to change it now that the ammount of players is not fixed anymore, I have a problem. I assume the problem is that the Arrays get initialized with 0, as numberOfPlayers is 0 at the beginning. How can I change this?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

3 Answers 3

3

You instantiate the arrays while numberOfPlayers still contains its default value of 0, so they are all empty arrays.

You should instantiate them in the constructor.

public Board(String[] s, int n) {   
    playerNames=s;
    numberOfPlayers= n;
    players = new Player[numberOfPlayers];
    playerNames= new String[numberOfPlayers];
    hands = new PlayerHand[numberOfPlayers];
}
Sign up to request clarification or add additional context in comments.

5 Comments

ok, i did this, but the Player has to be instantiated with name and hands. i tried it with for (int i = 0; i < numberOfPlayers; i++) { players[i]=new Player(playerNames[i],hands[i]); } But i'm getting a NullPointerException now.
@Allantir You should probably initialize playerNames[i] and hands[i] before initializing players[i].
it looks now like this: players = new Player[numberOfPlayers]; hands = new PlayerHand[players.length]; for (int i = 0; i < players.length; i++) { players[i]=new Player(playerNames[i],hands[i],); should i initialize them differently?
@Allantir Where do you initialize playerNames[i] and hands[i]?
nvm, i got it now, i instatiate them now in the constructor hands = new PlayerHand[numberOfPlayers]; and then run through the for Loop hands[i] = new PlayerHand(); the code works now! Am i doing anything unnecessary in it? it seems rather long. Other than that, thank you very much for the help
1

When you create the arrays, their sizes are 0 because numOfPlayers is 0 at that point.
You need to create initialize them in the constructor after you assign the player number a value

private int numberOfPlayers; 
private Player[] players;
private String[] playerNames;
private PlayerHand[] hands;
public Board(String[] s, int n) { 
    playerNames=s; 
    numberOfPlayers= n; 
    players = new Player[numberOfPlayers]; 
    playerNames= new String[numberOfPlayers]; 
     hands = new PlayerHand[numberOfPlayers];
}

Comments

1

The line

private String[] playerNames = new String[numberOfPlayers];

equals to

private String[] playerNames = new String[0];

because an instance field is initialized with a default value (0 for int).

You should move all initialization stuff to a controller where a size of arrays will be known. Furthermore, there is no need to keep a number of players (n) after array initialization (players.length will return the same):

public Board(String[] s, int n) {
    ...
    players = new Player[n];
}

1 Comment

that is probably the prettiest solution and i will do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.