英文:
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<Stat> stat = new List<Stat>();
List<Num>[] num = new List<Num>[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<Num>
的变量,你希望它默认是 null
还是自动包含一个类型为 List<Num>
的对象?你有这样的代码:
List<Stat> stat = new List<Stat>();
表明你知道它默认会是 null
,你必须实际分配一个对象给它,以使其不是 null
。数组中的每个元素都是一样的。除非你将 List<Num>
对象分配给一个元素,否则该元素将为 null
。
如果你想避免逐个创建 45 个单独的对象,可以这样做:
List<Num>[] num = Enumerable.Range(1, 45).Select(n => new List<Num>()).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<Num>
, would you expect it to be null
by default or automatically contain an object of type List<Num>
? The fact that you have this:
List<Stat> stat = new List<Stat>();
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<Num>
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<Num>[] num = Enumerable.Range(1, 45).Select(n => new List<Num>()).ToArray()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论