使用MyListProperty.Add时NullReferenceException?

时间:2012-06-24 05:46:37

标签: c# nullreferenceexception

 public List<string> myitems { get; set; }
    //Store All unique ID of this date in a list
    public void myitem(string Index)
    {
        myitems.Add(Index);
    }

  if (temp.Start == received)
            {

                scheduledItem scheduleditem = new scheduledItem(temp.Name, "1pm", "true");
                DataCollection.Add(scheduleditem);
                myitem(temp.UniqueID.ToString()); 
            }
            else
            {
                MessageBox.Show("Nothing");

            }

知道myitems.add(index)出了什么问题; ?

2 个答案:

答案 0 :(得分:4)

您必须实例化/初始化List<T>

 private List<string> _list = new List<string>();

 public List<string> myitems 
 { 
     get { return _list; }
 }

答案 1 :(得分:2)

你还没有真正初始化myitems。您需要在某处添加这行代码,例如类的构造函数

myitems = new List<string>();