我在C#中有此代码。 它返回“未设置对象实例的对象引用”异常。C#List +未将对象引用设置为对象的实例
代码:
public decimal Calculate(String id)
{
decimal Total=0;
AmountDataDB getData=new AmountDataDB();
List<AmountData> d = new List<AmountData>();
d = getData.Amount_Details(id);
if (d.Capacity != 0)
{
foreach (AmountData temp in d)//NullReference exception occurs here
{
Total += temp.Amount;
}
}
return Total;
}
这里,AmountDataDB和AmountData两类。 Amount_Details返回一个类型为AmountData的列表。
你AmountData对象可能是NULL。检查它 – 2010-06-21 11:10:13
你是否正面,Amount_Details不返回空? – Rup 2010-06-21 11:10:47
如果它返回null,那么'if(d.capacity ...)'应该抛出异常 – cjk 2010-06-21 11:15:48