C# 嵌套类型列表
我创建了一个包含类型列表的类,并派生到我创建的另一个类。 如下所示:
namespace MyIntegretyCheck.Common
{
/// <summary>
/// Description of PolicyErrors.
/// </summary>
public partial class PolicyErrorEndingDates
{
public int ID_P {get;set;}
public DateTime DT_S_E {get;set;}
public DateTime DT_SC_E {get;set;}
public List<PolicyErrorDescr> Errors {get;set;}
}
public partial class PolicyErrorDescr
{
public string Field1{get;set;}
public string Field2 {get;set;}
public string F1IsThanF2 {get;set;}
public string Message {get;set;}
public int ErrorLevel {get;set;} //0= Info | 1= Warning | 2= Error
}
}
现在我创建了一个 PolicyErrorEndingDates
的类型列表,添加了一个条目,然后尝试添加其嵌套列表的条目错误如下:
public List<PolicyErrorEndingDates> MyPolicyEndingDates()
{
DAL.PolicyEndingDates ped = new DAL.PolicyEndingDates();
List<PolicyErrorEndingDates> MyErrors = new List<PolicyErrorEndingDates>();
foreach(var m in ped.CheckTables())
{
bool HasError = false;
PolicyErrorEndingDates p = new PolicyErrorEndingDates();
p.ID_P = m.ID_P;
if(m.DT_S_E != m.DT_SC_E)
{
PolicyErrorDescr e = new PolicyErrorDescr();
HasError = true;
e.Field1 = "DT_S_E";
e.Field2 = "DT_SC_E";
e.Message = "blablabla...";
e.ErrorLevel = 3;
p.Errors.Add(e);
}
if(HasError)
MyErrors.Add(p);
}
}
调试器崩溃,并显示消息 Object reference not设置为对象的实例
,位于我的 if
内的 p.Errors.Add(e);
行。 我做错了什么? 如何创建嵌套列表的实例?
I've created a class witch contains a typed list and derives to another class that I created. This looks as follows:
namespace MyIntegretyCheck.Common
{
/// <summary>
/// Description of PolicyErrors.
/// </summary>
public partial class PolicyErrorEndingDates
{
public int ID_P {get;set;}
public DateTime DT_S_E {get;set;}
public DateTime DT_SC_E {get;set;}
public List<PolicyErrorDescr> Errors {get;set;}
}
public partial class PolicyErrorDescr
{
public string Field1{get;set;}
public string Field2 {get;set;}
public string F1IsThanF2 {get;set;}
public string Message {get;set;}
public int ErrorLevel {get;set;} //0= Info | 1= Warning | 2= Error
}
}
Now I created a typed list of PolicyErrorEndingDates
, added an entry and tried then to add entries of his nested list Errors as follows:
public List<PolicyErrorEndingDates> MyPolicyEndingDates()
{
DAL.PolicyEndingDates ped = new DAL.PolicyEndingDates();
List<PolicyErrorEndingDates> MyErrors = new List<PolicyErrorEndingDates>();
foreach(var m in ped.CheckTables())
{
bool HasError = false;
PolicyErrorEndingDates p = new PolicyErrorEndingDates();
p.ID_P = m.ID_P;
if(m.DT_S_E != m.DT_SC_E)
{
PolicyErrorDescr e = new PolicyErrorDescr();
HasError = true;
e.Field1 = "DT_S_E";
e.Field2 = "DT_SC_E";
e.Message = "blablabla...";
e.ErrorLevel = 3;
p.Errors.Add(e);
}
if(HasError)
MyErrors.Add(p);
}
}
The Debugger crashed with the message Object reference not set to an instance of an object
, at the line p.Errors.Add(e);
inside my if
. What did I do wrong? How can I create an instance of the nested list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否将
List
实例分配给 Errors 属性?首先,我可能会将
Errors
集合从外部设置为只读,即可以更改列表,但不能为其提供新列表。我将通过将 setter 设置为私有来实现:
然后我将创建一个构造函数并从中将一个集合实例分配给该属性:
这应该处理空引用异常。
Did you assign a
List<PolicyErrorDescr>
instance to the Errors property?First, I would probably make the
Errors
collection read-only from the outside, that is, the list can be changed, but you can't give it a new list.This I would do by making the setter private:
Then I would create a constructor and assign a collection instance to that property from it:
This should take care of the null reference exception.
我的猜测是,由于 p 是一个新实例,因此错误列表没有实例化(就像 Lasse 提到的那样)。
My guess, since p is a new instance, the Errors list wasn't instantiated (Like what Lasse mentioned).
永远不会初始化 PolicyErrorEndingDates 中的错误列表:
如果按如下方式更正,则
You never initialise the Errors list in the PolicyErrorEndingDates
if you correct as follows:
p.Errors 为空
p.Errors is null