我有一個State
的List<State>
實體對象,每個State
對象有其他對象的集合,如Licenses
,Taxes
,Bonds
等爲什麼我在System.Data.Entity.dll(實體框架)中得到NullReferenceException?
還有爲ExpiredObjects
集合,它是在任何對象的列表過期,需要更新。出於某種原因,當我嘗試訪問某個特定狀態(內華達州)時,此屬性給了我一個NullReferenceException
,但我無法爲我的生活找出實際上null
,因爲我沒有看到任何null
值任何地方。
這是我的代碼,引發異常。它循環遍歷所有狀態,並將所有ExpiredObjects
添加到顯示的特定於視圖的集合中。我的測試代碼仍然包含
private List<ExpiredObject> LoadAllExpiredObjects()
{
var list = new List<ExpiredObject>();
foreach (var state in States)
{
// This tells me the state is not null, and neither is state.ExpiredObjects
Debug.WriteLine(string.Format("{0}|{1}|{2}",
state.Name, state == null, state.ExpiredObjects == null));
try
{
var x = state.ExpiredObjects;
Debug.WriteLine(x == null);
// Exception occurs on the "for" line on the Nevada state only
// Contents in for loop do not execute
foreach (var item in x)
{
Debug.WriteLine(string.Format("{0}", item));
list.Add(item);
}
// This used to be the only line of code before I started testing
// It fails on Nevada
//list.AddRange(state.ExpiredObjects);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
}
}
return list;
}
錯誤的堆棧跟蹤是這樣的:
A first chance exception of type 'System.NullReferenceException' occurred in System.Data.Entity.dll
Object reference not set to an instance of an object.
at System.Data.EntityKey.AddHashValue(Int32 hashCode, Object keyValue)
at System.Data.EntityKey.GetHashCode()
at System.Collections.Generic.GenericEqualityComparer`1.GetHashCode(T obj)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& entry)
at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper)
at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Data.Objects.DataClasses.RelatedEnd.Merge[TEntity](IEnumerable`1 collection, MergeOption mergeOption, Boolean setIsLoaded)
at System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption)
at System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)
at System.Data.Objects.DataClasses.RelatedEnd.Load()
at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
at System.Data.Objects.DataClasses.EntityCollection`1.GetEnumerator()
at MyNamespace.StatesViewModel.LoadAllExpiredObjects() in C:\Users\me\Desktop\MySolution\StatesViewModel.cs:line 217
我也得到了完全相同的錯誤,當我選擇內華達州和它試圖將DataGrid
綁定到ExpiredObjects
收集(如果我註釋掉綁定,代碼工作正常)
有誰知道什麼可能導致此錯誤?
如果只有內華達它必須是一個數據問題,請在db徹底檢查。 –
@HenkHolterman對不起,我不確定什麼信息是相關的。這是db-first。所有對象('State',''License','Tax'等)都有自己的數據庫表,外鍵鏈接它們。他們也有自己的主鍵,沒有任何東西被繼承。我正在調查數據 - 不確定爲什麼我沒有想到在 – Rachel
@HenkHolterman之前這樣做你說得對,問題在於數據。我有過期的對象的過程是爲在EF中標記爲非空的字段之一返回一個「空值」,所以每當它嘗試從數據庫中獲取該對象時,都會拋出一個錯誤。謝謝,如果你張貼作爲答案我會接受:) – Rachel