0

I have the following line of code that calls into my Entity Framework and is returning a Null Reference Exception.

List<FieldGroup> fieldGroups = dataContext.FieldGroups.Where(fieldGroup => fieldGroup.Field_Catalog_Id == fieldVersion.FieldCatalogId && fieldGroup.Field_Catalog_Version == fieldVersion.FieldCatalogVersion).ToList();

fieldVersion is being generated earlier in the method and I have confirmed that it, and its fields FieldCatalogId and FieldCatalogVersion are not null and have proper values.

My context, dataContext, while poorly named is connecting to my database and it does have rows in the FieldGroups table. I have also confirmed that a row with Field_Catalog_Id and Field_Catalog_Version matching the values being requested in the where exist so it should be able to find rows to return.

I can't determine why the line is throwing a NullReferenceException. Any ideas are appreciated. Let me know if I can provide any more useful info.

Edit: I did take a look at the locals when the exception was being thrown and nothing that was being referenced was showing up null.

I added in Debug.Assert to check that dataContext, dataContext.FieldGroups, fieldVersion, fieldVersion.FieldCatalogId, and fieldVersion.FieldCatalogVersion are not null before the problem line. The code got past all 5 of those asserts and still threw a NullReferenceException. I can't find anything within those fields that is null that could be causing the exception.

Evan Frisch
  • 1,334
  • 5
  • 22
  • 40
  • What does it return without where? – Giorgi Nakeuri May 11 '15 at 19:59
  • Is your properties mapped correctly to their table columns? Is your table mapped correct to the table name? Is your `dataContext` null? – Jacob Roberts May 11 '15 at 20:00
  • `fieldVersion` is most likely null. – David L May 11 '15 at 20:01
  • 2
    Queries don't throw random NullReferenceException. Add `Debug.Assert(dataContext != null); Debug.Assert(dataContext.FieldGroups != null); Debug.Assert(fieldVersion != null); Debug.Assert(fieldVersion != null); ` before the query and see which one explodes – xanatos May 11 '15 at 20:01
  • You should be able to tell what's null when the exception is thrown. Hover over everything and try to see what's not looking right, look at locals... – Alex Paven May 11 '15 at 20:40
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch May 11 '15 at 22:06

0 Answers0