Null reference exception when adding entity to data context
<p>my entity relation is like Customer > Address > Contacts. each customer may have multiple addresses and each address may have multiple contacts.</p>

<h2>my Entity POCO classes as like</h2>

<pre><code>public class CustomerBase
{
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [NotMapped]
    public string Address1 { get; set; }

    [NotMapped]
    public string Address2 { get; set; }

    [NotMapped]
    public string Phone { get; set; }

    [NotMapped]
    public string Fax { get; set; }

}

public class Customer : CustomerBase
{
    public virtual List&lt;Addresses&gt; Addresses { get; set; }
}

public class Addresses
{
    [Key]
    public int AddressID { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public bool IsDefault { get; set; }
    public int SerialNo { get; set; }
    public virtual List&lt;Contacts&gt; Contacts { get; set; }

    public int CustomerID { get; set; }
    //[ForeignKey("CustomerID")]
    public virtual Customer Customer { get; set; }
}

public class Contacts
{
    [Key]
    public int ContactID { get; set; }

    public string Phone { get; set; }
    public string Fax { get; set; }
    public bool IsDefault { get; set; }
    public int SerialNo { get; set; }
    public int AddressID { get; set; }

    //[ForeignKey("AddressID")]
    public virtual Addresses Customer { get; set; } 

}
</code></pre>

<h2>My DB context look like</h2>

<pre><code>public class TestDBContext : DbContext
{
    public TestDBContext()
        : base("name=TestDBContext")
    {
        //((IObjectContextAdapter)this).ObjectContext.SavingChanges += new EventHandler(objContext_SavingChanges);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //modelBuilder.Configurations.Add(new vwCustomerConfiguration());
        Database.SetInitializer&lt;TestDBContext&gt;(null);
    }

    public DbSet&lt;Customer&gt; Customer { get; set; }
    public DbSet&lt;Addresses&gt; Addresses { get; set; }
    public DbSet&lt;Contacts&gt; Contacts { get; set; }
    public virtual DbSet&lt;vwCustomer&gt; vwCustomers { get; set; }
    public DbSet&lt;vwMyCustomers&gt; vwMyCustomers { get; set; }
}
</code></pre>

<p>Now here is code which is update parent and insert new child data into address and contact table.</p>

<pre><code>    using (var db = new TestDBContext())
    {
        //db.Database.Log = s =&gt; MyLogger.Log("EFApp", s);

        var existingCustomer = db.Customer
        .Include(a =&gt; a.Addresses.Select(x =&gt; x.Contacts))
        .FirstOrDefault(p =&gt; p.CustomerID == 5);

        existingCustomer.FirstName = "New Customer";

        existingCustomer.Addresses.Where(a =&gt; a.AddressID == 5).ToList().ForEach(r =&gt; db.Addresses.Remove(r));
        existingCustomer.Addresses.Where(a =&gt; a.AddressID == 5).SelectMany(ad =&gt; ad.Contacts).Where(c=&gt; c.ContactID==5).ToList().ForEach(r =&gt; db.Contacts.Remove(r));

        Addresses oAdrModel = new Addresses();
        oAdrModel.Address1 = "New test xxx";
        oAdrModel.Address2 = "New test xxx";
        oAdrModel.SerialNo = 3;
        oAdrModel.IsDefault = true;
        //oAdrModel.CustomerID = 5;
        existingCustomer.Addresses.Add(oAdrModel);
        //db.Addresses.Add(oAdrModel);
        //db.SaveChanges();
        //int CurAddressID = oAdrModel.AddressID;

        Contacts ContactModel = new Contacts();
        ContactModel.Phone = "New XX-1111111-33";
        ContactModel.Fax = "New XX-1-1111111";
        ContactModel.SerialNo = 4;
        ContactModel.IsDefault = true;
        oAdrModel.Contacts.Add(ContactModel);

        //ContactModel.AddressID = CurAddressID;
        //db.Contacts.Add(ContactModel);

        db.SaveChanges();
    }
</code></pre>

<p><code>oAdrModel.Contacts.Add(ContactModel);</code> throwing error called <strong>Object reference not set to an instance of an object.</strong> just do not understand what was the fault in code. </p>

<h2>this way it is fixed now</h2>

<pre><code>        oAdrModel.Contacts = new List&lt;Contacts&gt;();
        Contacts ContactModel = new Contacts();
        ContactModel.Phone = "New XX-1111111-33";
        ContactModel.Fax = "New XX-1-1111111";
        ContactModel.SerialNo = 4;
        ContactModel.IsDefault = true;
        oAdrModel.Contacts.Add(ContactModel);
</code></pre>