views:

360

answers:

4

I've created a class witch contains a typed list and derives to another class 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

    }
}

well now I created a typed list of PolicyErrorEndingDates, added a 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);
  }

}

But the Debugger creashed 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 wrong? How can I create a instance of the nested list?

Tanks in Advance Johannes

+5  A: 

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:

public List<PolicyErrorDescr> Errors { get; private set; }
                                            ^-----^
                                              this

Then I would create a constructor and assign a collection instance to that property from it:

public PolicyErrorEndingDates()
{
    Errors = new List<PolicyErrorDescr>();
}

This should take care of the null reference exception.

Lasse V. Karlsen
+1: wrote the same thing, just to realize you were quicker :o)
Fredrik Mörk
Thanks for the response I just completed my error description.
john84
A: 

p.Errors is null

malay
+1  A: 

My guess, since p is a new instance, the Errors list wasn't instantiated (Like what Lasse mentioned).

e.ErrorLevel = 3;
p.Errors = new List<PolicyErrorDescr>(); //add this
p.Errors.Add(e);
o.k.w
+1  A: 

You never initialise the Errors list in the PolicyErrorEndingDates

if you correct as follows:

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 PolicyErrorEndingDates()
        {
            Errors = new List<PolicyErrorDescr>()
        }
    }
Richard