0

I have found nothing to answer this question, and I think it is different for my code. Probably because I am adding a custom object to an array, but I still don't know what is causing this. The error is on line 35. Code attached below.

using System;

class Player {
    public string abilitySet;
    public string name;
    
    public void useAbility() {
        if (abilitySet == "fire") {
            Console.WriteLine(name + " used fireball which did 40 damage!");
        }
    }
}

class Match {
    public int PlayerCount;
    public Player[] players;

    public void Start() {
        Console.WriteLine("Game started! There are " + PlayerCount + " players!");
    }
}

class Program {
    public static void Main(string[] args) {
        Match match = new Match();
        Console.WriteLine("How many players are there?");
        string playerCount = Console.ReadLine();
        if (Int32.TryParse(playerCount, out int j)) {
            
            Console.WriteLine("You have selected " + playerCount + " players!");
            match.PlayerCount = j;
            for (int i = 0; i < j; i++) {
                Player plr = new Player();
                match.players[i] = plr;
                plr.name = "Player " + i;
                Console.WriteLine("What do you want " + plr.name + "'s ability set to be?");
                string ability = Console.ReadLine();
                if (ability.ToLower() == "fire") {
                    Console.WriteLine(plr.name + " has " + ability + "!");
                } else {
                    Console.WriteLine("That is not a ability!");
                }
            }
        } else {
            Console.WriteLine("Please enter a number of players not text!");
        }
    }
}
Yaumama
  • 17
  • 6
  • What is the error? Is the problematic line the following: `plr.name = "Player " + i;`? – Lajos Arpad Jan 09 '22 at 06:14
  • Thanks for telling us the line number. But, I'm not going to count off 35 lines of code to find it. In the future, add a comment lie `//error here, line 35` to the end of line 35 – Flydog57 Jan 09 '22 at 06:35

2 Answers2

0

Field players of Match class is not initialized. You should create an array:

match.players = new Player[j];

Or do it in constructor:

class Match {
    public int PlayerCount;
    public Player[] players;

    public Match(int count) {
        PlayerCount = count;
        players = new Player[count];
    }

    public void Start() {
        Console.WriteLine("Game started! There are " + PlayerCount + " players!");
    }
}

Match match = new Match(j);
Backs
  • 24,430
  • 5
  • 58
  • 85
  • This doesn't work as I am setting the playercount after making the object. This is the error I get: `Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.` – Yaumama Jan 09 '22 at 16:26
  • Turns out I am stupid. Thanks for the help! It worked! – Yaumama Jan 21 '22 at 04:55
0

As @Backs mentioned, you are not initializing your variables. But I would organize your code as follows and use a List instead of an array. Much easier to read don't you think?

using System;

class Player
{
    public Player(string name)
    {
        Name = name;
    }

    public string Name { get; set; } = String.Empty;
    public string AbilitySet { get; set; } = String.Empty;

    public void UseAbility()
    {
        if (AbilitySet == "fire")
        {
            Console.WriteLine(Name + " used fireball which did 40 damage!");
        }
    }
}

class Match
{
    public List<Player> Players { get; set; } = new List<Player>();

    public void Start()
    {
        Console.WriteLine("Game started! There are " + Players.Count + " players!");
    }
}

class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("How many players are there? Or type Q+ENTER to quit.");

        int numberOfPlayers = 0;

        while (true)
        {
            string input = Console.ReadLine();
            if (input == "Q")
            {
                System.Environment.Exit(1);
            }

            if (!Int32.TryParse(input, out numberOfPlayers))
            {
                Console.WriteLine("Invalid input. Please enter a numeric value.");
                continue;
            }

            if (numberOfPlayers <= 0)
            {
                Console.WriteLine("Number of players must be at least 1.");
                continue;
            }

            break;
        }
        
        Match match = new Match();
        for (int i = 0; i < numberOfPlayers; i++)
        {
            match.Players.Add(new Player($"Player {i + 1}"));
        }

        match.Start();
    }
}
Sandy
  • 1,284
  • 2
  • 14
  • 32