1

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;
1
  • what is the value of amountofPlayers. its 0 so if players [2] will throw error Commented Dec 3, 2013 at 6:48

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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!");
    }
}

Comments

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

Comments

2

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;

Comments

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.