2013-03-18 63 views
0

這是我第一次在StackOverflow中發佈一些東西(它在過去幫助過我很多),通常我都不會必須發佈一個問題,因爲我的假設問題已經得到了回答。但是這個我真的找不到合適的解釋。無論何時初始化其他程序集中的模型實例,都無法創建ViewModelLocator的實例

所以我在修補C#,Entity Framework 4,WPF和MVVM Light。我已經制作了一個程序集,作爲各種模型,視圖和視圖模型的庫,用於特定目的。

現在我正在製作另一個用於不同目的的組件。但是,在第一個程序集中已經提供了一些必需的方法和視圖。所以我想我要將第一個程序集的引用添加到第二個程序集,然後從第一個程序集創建Model的實例到第二個程序集的Model或ViewModel。

一切都沒有錯誤的構建。但是,當我檢查了第二組件的視圖的設計視圖,這部分有鋸齒狀下劃線:

<UserControl.Resources> 
    <vm:ViewModelLocator x:Key="Locator" /> 
</UserControl.Resources> 

,當你看設計圖,還有在與文本頂部欄寫的錯誤:

Object reference not set to an instance of an object. 
    at Powell.Materials.Common.ViewModel.MaterialsViewViewModel.SetupLists() in E:\Projects\Software\Powell Marketing\Powell\Powell\Materials\Powell.Materials.Common\ViewModel\MaterialsViewViewModel.cs:line 514 
    at Powell.Materials.Common.ViewModel.MaterialsViewViewModel..ctor() in E:\Projects\Software\Powell Marketing\Powell\Powell\Materials\Powell.Materials.Common\ViewModel\MaterialsViewViewModel.cs:line 601 

有人遇到過這種類型的錯誤嗎?或者有沒有一種合適的方式來做這種事情?

+0

你可以發佈'MaterialsViewViewModel.SetupLists()'嗎?我很確定,那個錯誤可以在那裏找到。 – DHN 2013-03-18 08:34:48

+0

'private void SetupLists() { Retrieve retrieve = new Retrieve(secretKey,IDUser,connectionName); unitListProperty = retrieve.RetrieveUnits(); matlTypeProperty = retrieve.RetrieveMaterialTypes(); supplierListProperty = retrieve.RetrieveSuppliers(); }' 它在自己的Model中調用一個方法,該方法與引用程序集中的Model進行通信。 – 2013-03-18 08:51:54

回答

0

那麼在代碼中,我看到4個可能的來源NullReferenceException

  • secretKey
  • IDUser
  • connectionName
  • retrieve(雖然這可能是正確實例)

我不知道從哪兒來的前三。所以檢查它們的值。我很確定其中有一個是null。如果所有三個都正確實例化,請檢查最後一個。

+0

它們在程序運行時立即實例化。但現在你已經提到過了,我想我應該爲每個聲明一個默認值。非常感謝:D – 2013-03-18 09:21:49