2013-12-21 17 views
1

當我運行它給我這個錯誤代碼:如何在使用兩個單獨的類時解決NullReferenceException?

An exception of type 'System.NullReferenceException' occurred in App.dll but was not handled in user code 

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

我將如何解決這個問題?

ExceptionHandling ExManager = new ExceptionHandling(); 
var Message = ExManager.ExceptionLibrary["10000"]// occured here 

類ExceptionHandling

public class ExceptionHandling 
{ 
    private Dictionary<string, string> exceptionlibrary; 
    public Dictionary<string, string> ExceptionLibrary 
    { 
     get { return exceptionlibrary; } 
     set 
     { 
      exceptionlibrary = new Dictionary<string, string>() 
      { 
       //User 
       {"10000", "Invalid user input."}, 
       {"10001", "Phone number is already registered."},        
      }; 
     } 
    } 
} 

回答

0

「set」永遠不會被調用,因此在「獲取」它時exceptionLibrary爲null。

也許這是一個更好的辦法:

public class ExceptionHandling 
{ 
    private Dictionary<string, string> exceptionlibrary = new Dictionary<string, string>  
     { 
      //User 
      {"10000", "Invalid user input."}, 
      {"10001", "Phone number is already registered."}, 
     }; 

    public Dictionary<string, string> ExceptionLibrary 
    { 
     get { return exceptionlibrary; } 
    } 
} 
0

根據您的代碼:

var Message = ExManager.ExceptionLibrary[10000]// occured here 

很顯然,ExManager.ExceptionLibrarynull,因爲它在二傳手錯誤實例化,這可能是永遠使用。您可以設置ExceptionLibrary在構造函數,然後是不再需要二傳手:

public class ExceptionHandling 
{ 
    public ExceptionHandling() 
    { 
     exceptionlibrary = new Dictionary<string, string>() 
     { 

      //User 
      {"10000", "Invalid user input."}, 
      {"10001", "Phone number is already registered."}, 


     }; 
    } 

    private Dictionary<string, string> exceptionlibrary; 
    public Dictionary<string, string> ExceptionLibrary 
    { 
     get { return exceptionlibrary; } 
    } 

注意:二傳手未使用value是最有可能使用不當,應再次rethinked。

0

這段代碼這裏

set 
{ 
    exceptionlibrary = new Dictionary<string, string>() 
     { 
      //User 
      {"10000", "Invalid user input."}, 
      {"10001", "Phone number is already registered."}, 
     }; 
} 

不被任何人調用,所以的ExceptionLibrary基準是null

您應該在引用此屬性之前先調用setter,或者可能在構造函數中進行初始化。

0

當值分配給屬性

ExManager.ExceptionLibrary = null; // For example 

因爲你不這樣做的屬性設置時,纔會激活你的字典中永遠不會被初始化。像這樣初始化它,並刪除setter,因爲你很可能不需要爲它指定另一個字典。

public class ExceptionHandling 
{ 
    private Dictionary<string, string> exceptionlibrary = 
     new Dictionary<string, string>() { 
      {"10000", "Invalid user input."}, 
      {"10001", "Phone number is already registered."}, 
     }; 

    public Dictionary<string, string> ExceptionLibrary 
    { 
     get { return exceptionlibrary; } 
    } 
} 

相反,你也可以使用延遲初始化

public Dictionary<string, string> ExceptionLibrary 
{ 
    get { 
     if (exceptionlibrary == null) { 
      exceptionlibrary = new Dictionary<string, string>() { 
       {"10000", "Invalid user input."}, 
       {"10001", "Phone number is already registered."}, 
      }; 
     } 
     return exceptionlibrary; 
    } 
} 

,或者你可以初始化在類的構造函數的字典。

0

正確的解決方案是通過在發生異常的行上設置斷點,然後重新調試應用程序/代碼來使用調試器。

當你遇到斷點時,你可以逐行檢查代碼,並沿途檢查變量。您可以通過鼠標光標在視覺工作室中檢查它們,或將它們拖到觀察窗口中。您也可以右鍵單擊並「添加監視」。

這種戰術應該解決任何異常類型的幫助,特別是當我的事業是幾層的代碼,或者當它是由事業在同一行的代碼的幾個表現之一。

希望有幫助!

相關問題