2011-04-28 44 views
0

好的,這個問題有點奇怪。取決於解決方案項目加載到哪個空引用異常?

我有一個內置於VS2010的應用程序,可以在自己的解決方案中正常運行。但是,如果我將項目加載到更大的解決方案(包含110個以上的項目)中,我開始得到NullReferenceExceptions,並且有時如果我繼續過去,它可以正常工作,而其他時間則不會。

但問題依然存在:爲什麼當我作爲更大的解決方案的一部分加載項目時,這些對象沒有被初始化,但是當它在它自己的解決方案中時,卻沒有被初始化?

編輯:下面是一個例外的樣本堆棧跟蹤:

A first chance exception of type 'System.NullReferenceException' occurred in DemoApp.exe 

Additional information: Object reference not set to an instance of an object. 

A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll 

Additional information: Exception has been thrown by the target of an invocation. 

System.Windows.Data Error: 17 : Cannot get 'ParentOrganDisplayName' value (type 'String') from '' (type 'OrganLocationViewModel'). BindingExpression:Path=ParentOrganDisplayName; DataItem='OrganLocationViewModel' (HashCode=19153159); target element is 'TextBox' (Name='organParentNameTxt'); target property is 'Text' (type 'String') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
    at DemoApp.ViewModel.OrganLocationViewModel.get_ParentOrganDisplayName() in C:\Visual Studio 2010\Projects\DemoApp\ViewModel\OrganLocationViewModel.cs:line 100 
    --- End of inner exception stack trace --- 
    at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
    at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) 
    at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level) 
    at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)' 
+0

哪裏例外?什麼是堆棧跟蹤? – SLaks 2011-04-28 14:25:43

回答

0

嗯,我發現它拋出異常的原因是在更大的解決方案中,而不是解決方案中的問題本身:當異常發生時,解決方案本身不會中斷!我把異常轉化爲事實並且確實如此,在同一個地方拋出了相同的異常。

0

根據堆棧跟蹤,ParentOrganDisplayName的獲取方法都有一個NRE。所以你應該檢查那裏正在做什麼。

也許在您更大的解決方案中,您使用的應用程序稍有不同?或者(由於更多的事情發生)一切都有點延遲,你的ViewModel(或別的東西)還沒有正確初始化(也稱爲競態條件)。

+0

有沒有適當的方法來處理競爭條件?對於拋出異常的地方之一,getter只返回相應的屬性。我只是添加了if條件,如果對象爲null,則返回一個新對象。這似乎已經解決了這個問題,但這似乎不是一個好的解決方案...... – Saggio 2011-04-28 15:35:46

相關問題