【问题标题】:Object reference not set to an instance of an object with arrays c# [duplicate]对象引用未设置为具有数组c#的对象的实例[重复]
【发布时间】:2022-01-09 06:00:20
【问题描述】:

我没有找到任何东西可以回答这个问题,而且我认为这与我的代码不同。可能是因为我将自定义对象添加到数组中,但我仍然不知道是什么原因造成的。错误在第 35 行。代码附在下面。

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

【问题讨论】:

  • 错误是什么?有问题的行是否如下:plr.name = "Player " + i;?
  • 感谢您告诉我们行号。但是,我不会数数 35 行代码才能找到它。以后在第 35 行末尾添加注释 lie //error here, line 35

标签: c# class


【解决方案1】:

正如@Backs 提到的,您没有初始化变量。但我会按如下方式组织您的代码并使用列表而不是数组。你不觉得更容易阅读吗?

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();
    }
}

【讨论】:

    【解决方案2】:

    Match 类的字段 players 未初始化。你应该创建一个数组:

    match.players = new Player[j];
    

    或者在构造函数中做:

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

    【讨论】:

    • 这不起作用,因为我在制作对象后设置了玩家数量。这是我得到的错误:Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
    • 原来我很愚蠢。谢谢您的帮助!成功了!
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2013-11-28
    • 1970-01-01
    相关资源
    最近更新 更多