Class Initialization Problem RRS feed

  • Question

  • Hello

    I have two class as below:

       public class Master
            public int Mid { get; set; }
            public string Mname { get; set; }
            public List<Child> Children { get; set; }
      public partial class Child
            public int CId { get; set; }
            public string Cname { get; set; }
            public Master Master { get; set; }

    If I want to perform any operation e.g. Insert as below:

    public int save()
            Child child = new Child();
            int i = context.InsertOrUpate(child.CId, child.Cname, child.Master.Mid);
            return i;

    It showing me Object Reference not set to the reference of an Object.

    Can anybody please help me to solve this problem?

    Thanks in advance

    Md. Mojammel Haque Senior Software Engineer Skype: mojamcpds1 Gmail:mojamcpds@gmail.com Cell: +8801817045882

    Wednesday, March 27, 2013 10:04 AM

All replies

  • The problem is line 2
    So error is occurring.. instead of that you can write abstract insert or update method in base class..
    Monday, April 1, 2013 6:47 AM
  • Your problem is  in the line

     int i = context.InsertOrUpate(child.CId, child.Cname, child.Master.Mid);

    child.Master is still null. I.e. child.Master.Mid will fail with an exception.
    • Proposed as answer by Loesche Friday, April 5, 2013 2:46 PM
    Friday, April 5, 2013 7:03 AM