空数组导致System.NullReferenceException。

huangapple go评论81阅读模式
英文:

Blank Array Causing System.NullReferenceException

问题

空数组导致 System.NullReferenceException: '对象引用未设置为对象的实例。'

所以我有两个类:

    internal class Stat
    {
        public int index { get; set; }
        public string StatAb { get; set; }
        public string L0 { get; set; }
        public string L1 { get; set; }
        public string L2 { get; set; }
        public string L3 { get; set; }
        public string L4 { get; set; }
    }
    internal class Num
    {
        public int index { get; set; }
        public int StatIndex { get; set; }
        public string StatG { get; set; }
        public string Date { get; set; }
        public string DE { get; set; }
        public string Num { get; set; }
    }

引用:

        List<Stat> stat = new List<Stat>();
        List<Num>[] num = new List<Num>[45];

我试图将 num 转换为数组

现在出现错误,说 numbers[0] 为空(不显示字段)

num[0].Add(new Num()
                        {
                            index = Convert.ToInt32(reader[0]),
                            StatIndex = Convert.ToInt32(reader[1]),
                            StatG = reader[2].ToString(),
                            Date = reader[3].ToString(),
                            DE = reader[5].ToString(),
                            Num = reader[4].ToString(),
                        });

当我键入这个时似乎可以工作(我可以使用 StateG 键入它)

num[5][3].StateG

,但尝试添加时出错

英文:

Blank Array Causing System.NullReferenceException: 'Object reference not set to an instance of an object.'

So I have two classes:

    internal class Stat
    {
        public int index { get; set; }
        public string StatAb { get; set; }
        public string L0 { get; set; }
        public string L1 { get; set; }
        public string L2 { get; set; }
        public string L3 { get; set; }
        public string L4 { get; set; }
    }
    internal class Num
    {
        public int index { get; set; }
        public int StatIndex { get; set; }
        public string StatG { get; set; }
        public string Date { get; set; }
        public string DE { get; set; }
        public string Num { get; set; }
    }

references:

        List&lt;Stat&gt; stat = new List&lt;Stat&gt;();
        List&lt;Num&gt;[] num = new List&lt;Num&gt;[45];

I'm trying to turn num into an array

this is now having an error saying numbers[0] is null (it doesn't show the fields)

num[0].Add(new Num()
                        {
                            index = Convert.ToInt32(reader[0]),
                            StatIndex = Convert.ToInt32(reader[1]),
                            StatG = reader[2].ToString(),
                            Date = reader[3].ToString(),
                            DE = reader[5].ToString(),
                            Num = reader[4].ToString(),
                        });

when I type this it seems to work (it can use the StatG when I type it)

num[5][3].StateG

, but error trying to add

答案1

得分: 0

当然是 null。你从未向其中放入任何内容,那你怎么能从中取出任何内容呢?将数组视为一组变量。数组中的每个元素基本上都是一个变量。如果你声明了一个类型为 List&lt;Num&gt; 的变量,你希望它默认是 null 还是自动包含一个类型为 List&lt;Num&gt; 的对象?你有这样的代码:

List&lt;Stat&gt; stat = new List&lt;Stat&gt;();

表明你知道它默认会是 null,你必须实际分配一个对象给它,以使其不是 null。数组中的每个元素都是一样的。除非你将 List&lt;Num&gt; 对象分配给一个元素,否则该元素将为 null

如果你想避免逐个创建 45 个单独的对象,可以这样做:

List&lt;Num&gt;[] num = Enumerable.Range(1, 45).Select(n =&gt; new List&lt;Num&gt;()).ToArray()
英文:

Of course it's null. You never put anything into it so how could you get anything out of it? Think of an array as a group of variables. Each element in the array is basically a variable. If you declared a variable of type List&lt;Num&gt;, would you expect it to be null by default or automatically contain an object of type List&lt;Num&gt;? The fact that you have this:

List&lt;Stat&gt; stat = new List&lt;Stat&gt;();

demonstrates that you know it would be null by default and that you have to actually assign an object to it in order for it to not be null. Every element in your array is the same. Until you assign a List&lt;Num&gt; object to an element, that element is null.

If you want to avoid having to create 45 separate objects individually, you can do something like this:

List&lt;Num&gt;[] num = Enumerable.Range(1, 45).Select(n =&gt; new List&lt;Num&gt;()).ToArray()

huangapple
  • 本文由 发表于 2023年5月28日 09:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定