user3055553
user3055553

Reputation: 55

Indexoutofrangeexception in C#

Can anyone help me to figure out the problem?

struct Player
{
    public string Name;
    public int X;
    public int Y;
}
static Player[] players = new Player[amountofPlayers];

static void ResetGame() {
    Console.WriteLine("Welcome to the game!");
    Console.Write("How many player will be taking part today: ");
    string playerNo = Console.ReadLine();
    amountofPlayers = int.Parse(playerNo);
    if (amountofPlayers <= 4)
    {
        for (int i = 0; i < amountofPlayers; i = i + 1)
        {
            int displayNumber = i + 1;
            Console.Write("Please enter the name of player " + displayNumber + ": ");
            players[i].Name = Console.ReadLine(); //error is here
        }
    }
    else
    {
        Console.WriteLine("Please enter a number of players less than 4!");
    }
}

static int amountofPlayers;

Upvotes: 1

Views: 144

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1500425

This line:

static Player[] players = new Player[amountofPlayers];

is executing before you assign a value to amountOfPlayers based on the user input. So amountOfPlayers has its default value of 0, and you're creating an array of 0 elements.

I suggest you just declare the variable:

 static Player[] players;

then make amountOfPlayers a local variable in your ResetGame method, initializing the array after you've asked how many players there are:

static void ResetGame() {
    Console.WriteLine("Welcome to the game!");
    Console.Write("How many player will be taking part today: ");
    string playerNo = Console.ReadLine();
    int amountofPlayers = int.Parse(playerNo);
    players = new Player[amountOfPlayers];
    ...
}

I' also suggest making Player a class instead of a struct, keeping fields private, and using properties instead.

Upvotes: 6

Jens Meinecke
Jens Meinecke

Reputation: 2940

players is initialised before amountofPlayers is set up (ie when amountofPlayers is still zero)

Seeing that you only want to allow up to 4 players set up, you might want to initialise amountofPlayers to 4:

static int amountofPlayers = 4;

Upvotes: 2

Indranil.Bharambe
Indranil.Bharambe

Reputation: 1498

    struct Player
        {
            public string Name;
            public int X;
            public int Y;
        }
        static Player[] players = new Player[amountofPlayers];


        static void ResetGame() {
            Console.WriteLine("Welcome to the game!");
            Console.Write("How many player will be taking part today: ");
            string playerNo = Console.ReadLine();
            amountofPlayers = int.Parse(playerNo);
         Array.Resize(ref  players, amountofPlayers ); /// this will resize your array element to the accepted amount of player
                if (amountofPlayers <= 4)
                {
                    for (int i = 0; i < amountofPlayers; i = i + 1)
                    {
                        int displayNumber = i + 1;
                        Console.Write("Please enter the name of player " + displayNumber + ": ");
                        players[i].Name = Console.ReadLine(); //error is here
                    }
                }
                else
                {
                    Console.WriteLine("Please enter a number of players less than 4!");
                }
        }

        static int amountofPlayers;

Upvotes: 2

gzaxx
gzaxx

Reputation: 17590

Your array of players is initialized to 0 because it is static and at first your numberofPlayers is 0. You have to initialize it when you obtain number of players.

static Player[] players;
static int amountofPlayers;

static void ResetGame()
{
    Console.WriteLine("Welcome to the game!");
    Console.Write("How many player will be taking part today: ");
    string playerNo = Console.ReadLine();
    amountofPlayers = int.Parse(playerNo);

    if (amountofPlayers <= 4)
    {
        players = new Player[amountofPlayers];
        for (int i = 0; i < amountofPlayers; i = i + 1)
        {
            int displayNumber = i + 1;
            Console.Write("Please enter the name of player " + displayNumber + ": ");
            players[i].Name = Console.ReadLine(); //error is here
        }
    }
    else
    {
        Console.WriteLine("Please enter a number of players less than 4!");
    }
}

Upvotes: 2

Related Questions