none
SubmitChanges -> {"Object reference not set to an instance of an object."} RRS feed

  • Question

  • Hello

     

    I have this code:

     

    AutoverhuurDataContext dc = new AutoverhuurDataContext();

    dc.Merks.Add(merk);

    dc.SubmitChanges();

     

     

    When i call dc.SubmitChanges() i get a

      Message "Object reference not set to an instance of an object." string

      StackTrace "   at System.Data.Linq.EntitySetDefValueAccessor`2.GetValue(T instance)\r\n   at System.Data.Linq.Provider.MetaAccessor`2.GetBoxedValue(Object instance)\r\n   at System.Data.Linq.CommonDataServices.<GetRelations>d__0.MoveNext()\r\n   at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)\r\n   at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects()\r\n   at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)\r\n   at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)\r\n   at System.Data.Linq.DataContext.SubmitChanges()\r\n   at PIH.Autoverhuur.DataAccess.MerkDA.InsertMerk(Merk merk) in D:\\Autoverhuur\\PIH.Autoverhuur.DataAccess\\MerkDA.cs:line 18" string

     

    what can be the problem ?

     

    kind regards

    Dieter

    • Moved by Noam Ben-Ami - MSFT1 Wednesday, September 23, 2009 9:39 PM (From:ADO.NET Entity Framework and LINQ to Entities)
    Friday, June 29, 2007 12:36 PM

Answers

  • Looks like you've hit a bug in LINQ to SQL that is expecting all entityset properties to be initialized (not null).
    Sunday, July 1, 2007 1:56 AM
    Moderator

All replies

  • Looks like you've hit a bug in LINQ to SQL that is expecting all entityset properties to be initialized (not null).
    Sunday, July 1, 2007 1:56 AM
    Moderator
  • the strange thing is that i initialized al properties of the object ?
    Sunday, July 1, 2007 9:39 AM
  • I've had the same problem here, but I believe I've found the solution.

    In my case, I had a custom property (i.e. not generated) in a partial class of an entity. This property was readonly. So, when Linq tried to get its setter, it came null.

    I wrote an empty setter and everything worked fine.

    I know it's been a while since this thread was open, but I think it'd be nice to leave a workaround for this problem in case other people have it.

    Cheers,

    Rachel Carvalho
    Monday, September 21, 2009 1:55 PM
  • My problem was similar, but it occurred intermittently when calling SubmitChanges. I'd get a NullReferenceException like:

    Unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Data.Linq.ChangeTracker.StandardChangeTracker.IsFastTracked(Object obj)
       at System.Data.Linq.ChangeTracker.StandardChangeTracker.GetTrackedObject(Object obj)
       at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)
       at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)
       at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)
       at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited)
       at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects()
       at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
       at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
       at System.Data.Linq.DataContext.SubmitChanges()

    I changed all ReadOnly properties to include an empty Setter, and haven't seen the issue again.

    HTH,

    Pete Anfinsen

    Friday, July 30, 2010 6:48 PM