C# 无法克服System.NullReferenceException

C# 无法克服System.NullReferenceException,c#,nullreferenceexception,C#,Nullreferenceexception,我有以下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Zad_8.Chapter11 { class Program { static void Main(string[] args) { Cat[] cats

我有以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zad_8.Chapter11
{
    class Program
    {
        static void Main(string[] args)
        {
             Cat[] cats = new Cat[10];
             for (int i = 0; i < 10; i++)
             {
                 cats[i].Name = "Cat " + sequence.NextValue();
             }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(cats[i].Name);
            }
        }
    }
}
这个类也在我的命名空间第11章中

namespace Zad_8.Chapter11
    {
         public class Cat
        {
        private string name = "Cat";
        private string color;

         public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        public Cat(string name)
        {
            this.name = name;
        }
    }
} 
namespace Zad_8.Chapter11
{
    public class sequence
    {
         private static int current = 0;

         private sequence()
        { }

    public static int NextValue()
       {
        current++;
        return current;
       }
    }
}
我的目标是创建10个object-Cat类型,并给它们命名为CatN,其中N是它们的编号

我有类Cat+name属性+get和set。 我有类序列,它会给我从1到10的数字

在Main函数中,我想创建数组类型Cat和for循环,以便在这个10索引数组中的每个元素上按顺序给出名称Cat+其编号

我的错误列表中没有错误,我尝试了很多事情,比如只打印10次猫等等。。但每次它抛出这个异常'System.NullReferenceException'

我读了MSDN,但大多数东西都不够好,无法克服这个问题

我也尝试了f11调试模式,似乎一切都很好,但当它开始读取带有cats[I]的行时;可以继续,只需坚持下去,编译器就不能继续了。我在数组中看到这一点,任何索引都是空的,我不确定,但这可能是问题所在

谁能告诉我怎么做才对吗

我想这是一个烦人又无聊的问题,但我是新手,必须要解决这个问题,所以请不要讨厌这篇文章。

cats[I]是空的,因为对象构造函数将在每个新槽中放置默认值……对于引用类型来说,它是空的。无法引用空对象的名称

将cat对象作为一个整体创建并将其作为一个整体插入

更新后的代码是什么样子的:


必须先创建Cat对象。将for循环替换为以下内容:

for (int i = 0; i < 10; i++)
{
     cats[i] = new Cat("Cat " + sequence.NextValue());
}

当你创建一个猫的数组时

Cat[] cats = new Cat[10];
您只是创建了一个可以包含猫的数组。但实际上那里没有猫,除非你把它们放进去

cats[i].Name = "Cat " + sequence.NextValue();
在这里,你需要数组中的'ith'项,它是null,那里没有猫,并且试图用null做一些事情。这就是造成你问题的原因

你得先把猫放进去

    cats[i] = new Cat("Cat " + sequence.NextValue());
更改此项:

Cat[] cats = new Cat[10];
for (int i = 0; i < 10; i++)
{
  cats[i].Name = "Cat " + sequence.NextValue();
}
为此:

Cat[] cats = new Cat[10];
for (int i = 0; i < 10; i++)
{
  cats[i] = new Cat(); //<------This line is new
  cats[i].Name = "Cat " + sequence.NextValue();
}

创建数组时,它不会自动实例化创建新对象。您需要特别告诉它这样做。

您还没有创建对象cat。 您可以使用空值初始化数组,因为Cat是引用类型

Cat[] cats = new Cat[10];
for (int i = 0; i < 10; i++)
{
    cats[i] = new Cat("Cat " + sequence.NextValue());
}

Cat是类,类是引用类型。引用类型使用空值初始化。引用类型数组将是空值数组。所以对于每只猫,你需要一只新的猫。非常感谢你的快速回答!谢谢你的快速回复@斯托扬佩特科夫一定要把最有帮助的回答标记为你可能需要等待几分钟的答案谢谢你的帮助!谢谢你的回答!我做到了,谢谢你的帮助:
Cat[] cats = new Cat[10];
for (int i = 0; i < 10; i++)
{
    cats[i] = new Cat("Cat " + sequence.NextValue());
}