int PlayerAmount = 10;
int CurrentPlayer = 0;
Player[] player = new Player[PlayerAmount];
while (true)
{
string Input;
Console.WriteLine("What would you like to do?");
Console.WriteLine("1. Create new player and score.");
Console.WriteLine("2. Display Highscores.");
Console.WriteLine("3. Write out to XML file.");
Console.Write("Input Number: ");
Input = Console.ReadLine();
if (Input == "1")
{
if (CurrentPlayer >= PlayerAmount)
{
Console.WriteLine();
Console.WriteLine("MAX AMOUNT OF PLAYERS HAS BEEN REACHED!");
Console.WriteLine();
}
else
{
string PlayerName;
string Score;
Console.WriteLine();
Console.WriteLine("-=CREATE NEW PLAYER=-");
Console.Write("Player name: ");
PlayerName = Console.ReadLine();
Console.Write("Player score: ");
Score = Console.ReadLine();
//=========================================
//THIS IS WHERE THE ERROR OCCURS===========
//=========================================
player[CurrentPlayer].Name = PlayerName;
Console.WriteLine("Player \"" + player[CurrentPlayer].Name + "\" with the score of \"" + player[CurrentPlayer].Score + "\" has been created successfully!" );
Console.WriteLine();
}
}
else if (Input == "2")
{
Console.WriteLine("Displaying the highscores");
}
else if (Input == "3")
{
Console.WriteLine("Writing to XML file");
}
else
{
Console.WriteLine("INVALID INPUT");
}
}
I’ve already tried to use string copy but that just gives me the same error, i’ve already pointed the problem out in the code, but if you can’t see it its where the player name is being copied: “player[CurrentPlayer].Name = PlayerName;”
Thanks in advance
You don’t initialize each member of the
Playerarray. After doing:you may want to do this as well:
Because beforehand, the array contains a bunch of
nullreferences only.