使用NUnit测试用下面的代码块C#代码:空对象引用
foreach (XmlNode node in nodeList)
{
thisReport.Id = node.Attributes.GetNamedItem("id").Value;
thisReport.Name = node.Attributes.GetNamedItem("name").Value;
thisReport.Desc = node.Attributes.GetNamedItem("desc").Value;
if (node.SelectNodes("subreport").Count > 0)
{
thisReport.HasSubReport = true;
subReportNodeList = node.SelectNodes("subreport");
foreach(XmlNode subNode in subReportNodeList)
{
mySubReport.ParentID = node.Attributes.GetNamedItem("id").Value;
mySubReport.Priority = subNode.Attributes.GetNamedItem("priority").Value;
mySubReport.SubReportId = subNode.Attributes.GetNamedItem("id").Value;
mySubReport.SubReportName = subNode.Attributes.GetNamedItem("name").Value;
string sTime = subNode.Attributes.GetNamedItem("time").Value;
mySubReport.Time = Convert.ToInt16(sTime);
thisReport.SubReportsList.Add(mySubReport);
}
}
else
{
thisReport.HasSubReport = false;
}
reports.Add(thisReport);
}
的代码失败就行了空对象引用:
thisReport.SubreportsList.Add(mySubReport)
但看当地人, thisReport
存在并且具有在块顶部分配的值,并且存在mySubReport
并且其值被添加到thisReport的行的上方。 mySubReport
中的所有值都是有效的,SubReportsList
中的thisReport
是SubReport
类型的通用列表。
那么,零位在哪里呢?这看起来很简单,它一定是我看不到的真正明显的东西。
如果'thisReport.SubReportsList.Add(mySubReport);'和'thisReport'和'mySubReport'中出现异常,那么唯一的选择是'thisReport.SubReportsList'为null。仔细检查你的财产实施。 – dtb 2010-09-16 15:02:07
[在.NET中是什么是NullReferenceException?]的可能重复(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – 2012-05-28 12:58:15