2013-02-23 54 views
-3

我收到類型的未處理的異常列表<T>。新增對象的類型給出了「System.NullReferenceException」的未處理的異常出現在UgenityAdministrationConsole.exe

「System.NullReferenceException」發生在 UgenityAdministrationConsole.exe

附加信息:未將對象引用設置爲 對象的實例。

這發生在我的類構造函數中。

這裏是我的代碼:

public static object dummyObject = new object(); // create a dummy object to use for initializing various things 

    public class EntityValuesClass 
    { 
     public List<EntityValue> EntityValues { get; set; } 

     public EntityValuesClass(EntityType _entType) 
     { 
      Type t; 
      PropertyInfo[] propInfoArray; 
      EntityValue entValue = new EntityValue(); 

      t = entityTypeToType[_entType]; 
      propInfoArray = t.GetProperties(); 

      foreach (PropertyInfo propItem in propInfoArray) 
      { 
       entValue.FieldName = propItem.Name; 
       entValue.FieldValue = dummyObject; 
       EntityValues.Add(entValue); <------ this is where the error is happening 
      } 
     } 
    } 


    public class EntityValue 
    { 
     public string FieldName { get; set; } 
     public object FieldValue { get; set; } 
    } 
+1

您沒有將對象引用(EntityValues)設置爲對象的實例(新List 2013-02-23 21:31:11

+0

http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – 2013-02-23 21:32:01

回答

1

EntityValuesnull因爲你不分配任何東西給它。

您可以將EntityValues = new List<EntityValue>();添加到您的構造函數的起始處以初始化它。

2

EntityValues爲空。你從來沒有初始化它。

2

您必須初始化EntityValue財產第一:

EntityValues = new List<EntityValue>(); 

在另一方面,根據CA1002: Do not expose generic lists你應該考慮改變你的類:

private List<EntityValue> _entityValues = new List<EntityValue>(); 
public List<EntityValue> EntityValues 
{ 
    get { return _entityValues; } 
} 
相關問題