【发布时间】:2014-08-04 09:36:22
【问题描述】:
class Program
{
static void Main(string[] args)
{
Posting onjPosting = null;
List<Posting> objList = null;
for (int i = 0; i < 100; i++)
{
onjPosting = new Posting();
onjPosting.Key1 = i;
for (int j = 0; j < 5; i++)
{
Choice objChoice = new Choice();
objChoice.ID = i;
objChoice.VAL = j;
onjPosting.GetPostingChoice.Add(objChoice); // GETTING ERROR [ Object reference not set to an instance of an object. ]
}
objList.Add(onjPosting);
}
}
}
public class Choice
{
public int ID { get; set; }
public int VAL { get; set; }
}
public class Posting
{
public int Key1 { get; set; }
public List<Choice> GetPostingChoice { get; set; }
}
在循环并分配值时出现错误。如何解决这个问题?请帮帮我。
我的要求是一个父类(Posting),可以包含多个数据 List 。
提前致谢。
【问题讨论】:
-
GetPostingChoice为空。使用前先初始化……更好,学习如何调试程序…… -
什么错误?你赋予什么价值?
-
顺便说一句。调用属性
Get...不利于代码的可读性。人们会期望一种方法而不是属性 -
错误,对象引用未设置为对象的实例。你能帮我看看创建实例的语法吗?
-
你能分享一个例子吗?
标签: c# asp.net winforms c#-4.0 console