我有以下的C#代碼片段對象refrence錯誤
public class Customer
{
public Customer()
{
PhoneList = new List<PhoneNumber>();
}
public Customer(int id, string name)
{
this.CustomerID = id;
this.CustomerName = name;
}
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public List<PhoneNumber> PhoneList { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class PhoneNumber
{
public int ID { get; set; }
public int Number { get; set; }
public PhoneNumber()
{
}
public PhoneNumber(int id, int number)
{
this.ID = id;
this.Number = number;
}
}
我使用的UI這些類爲波紋管
protected void Page_Load(object sender, EventArgs e)
{
List<Customer> list = new List<Customer>();
list.Add(new Customer(2, "John"));
list.Add(new Customer(3, "Joe"));
list.Add(new Customer(4, "Don"));
**list[0].PhoneList.Add(new PhoneNumber(1, 1231213));//Object reference not set to an instance of an object.**
list[0].PhoneList.Add(new PhoneNumber(1, 1231213));
GridView1.DataSource = list;
GridView1.DataBind();
}
當我正在執行獲取的頁面時未將對象引用設置爲obj的實例等。錯誤消息。
+1,我無法輸入那麼快! – Pondidum 2011-04-28 09:14:45
+1爲快速解決方案 – 2011-04-28 09:22:24