空对象引用
使用 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
存在,并且在其添加到此Report 的行上方分配了值。 mySubReport
中的所有值均有效,并且 thisReport
中的 SubReportsList
是 SubReport
类型的通用列表。
那么,空值在哪里呢?看起来很简单,一定是一些很明显但我看不到的东西。
Using Nunit to test C# code with the following code block:
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);
}
The code fails with a null object reference on the line:
thisReport.SubreportsList.Add(mySubReport)
But looking at the locals, thisReport
exists and has the values assigned at the top of the block, and mySubReport
exists and has the values assigned just above the line where it's added to thisReport. All the values in mySubReport
are valid and SubReportsList
in thisReport
is a generic list of type SubReport
.
So, where's the null? It seems so simple, it must be something really obvious that I can't see.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在调用
Add
之前您尚未实例化 SubReportsList。在添加mySubReport
之前执行以下操作:您还可以更改 SubReportsList 属性以使您的生活更轻松:
如果在 List 为 null 时调用它,则执行此操作会实例化您的 List。
You've not instantiated SubReportsList before calling
Add
. Do the following before addingmySubReport
:You could also change your SubReportsList property to make your life easier:
Doing this would instantiate your List if it's called while it's null.
你可能首先应该这样做:
You should probably first do:
那么它一定是
SubReportsList
为 null。It must be
SubReportsList
that is null then.由于 @GenericTypeTea 和 @Dan Dumitru 已经提供了很好的答案,我将补充一点,如果在调用属性时值为 null,则可以通过添加隐式构造来“自动”执行此操作。如果您不使用自动属性,则可以执行此操作:
有一些注意事项需要注意,例如使其成为线程安全的(这是即兴的),但基本实现将适合您。我会小心使用这种设计,因为它可能会导致仅通过检查属性就创建不一定需要的对象。如果这是不可取的,那么请坚持上面推荐的实现。
As @GenericTypeTea and @Dan Dumitru have already provided good answers I will just add that it is possible to "automatically" do this by adding an implicit construction if the value is null when you call the property. You can do this if you are not using auto-properties ala:
There are some caveats to be noted, like making it thread-safe (this is off-the-cuff), but the basic implementation will work for you. I would be careful using this desing as it can cause the creation of objects you don't necessarily need just by checking properties. If that is undesirable then stick with the implementations recommended above.
thisReport.SubReportsList
是您的空引用;你已经声明了它但没有初始化它。您可以在thisReport
类型的构造函数中初始化它(可能使用新实例),或者在开始向其中添加内容之前初始化它。thisReport.SubReportsList
is your null reference; you've declared it but not initialized it. You can initialize it (probably with a new instance) either in a constructor forthisReport
's type, or just before you start adding things to it.确保使用
new
关键字初始化列表。否则列表本身将为空。Make sure to initialize the list by using the
new
keyword. Otherwise the list itself will be null.