C# 列表<;T>;。添加对象会产生类型为';的未处理异常;System.NullReferenceException';在UgenityAdministrationConsole.exe中发生
我收到类型为的未处理异常 中发生“System.NullReferenceException” UgenityAdministrationConsole.exe 其他信息:对象引用未设置为 反对 这发生在我的类构造函数中 这是我的密码:C# 列表<;T>;。添加对象会产生类型为';的未处理异常;System.NullReferenceException';在UgenityAdministrationConsole.exe中发生,c#,exception,object,initialization,nullreferenceexception,C#,Exception,Object,Initialization,Nullreferenceexception,我收到类型为的未处理异常 中发生“System.NullReferenceException” UgenityAdministrationConsole.exe 其他信息:对象引用未设置为 反对 这发生在我的类构造函数中 这是我的密码: public static object dummyObject = new object(); // create a dummy object to use for initializing various things public cl
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; }
}
公共静态对象dummyObject=new object();//创建一个虚拟对象,用于初始化各种内容
公共类实体值类
{
公共列表EntityValues{get;set;}
public EntityValuesClass(EntityType\u entType)
{
t型;
PropertyInfo[]ProInfo数组;
EntityValue entValue=新的EntityValue();
t=entityTypeToType[_entType];
PropInfo数组=t.GetProperties();
foreach(PropInfo数组中的PropertyInfo ProItem)
{
entValue.FieldName=ProItem.Name;
entValue.FieldValue=dummyObject;
EntityValues.Add(entValue);EntityValues
为空。您从未初始化过它。您必须先初始化EntityValue
属性:
EntityValues = new List<EntityValue>();
EntityValues=newlist();
另一方面,根据你应该考虑把你的班改为:
private List<EntityValue> _entityValues = new List<EntityValue>();
public List<EntityValue> EntityValues
{
get { return _entityValues; }
}
private List _entityValues=new List();
公共列表EntityValues
{
获取{return\u entityValues;}
}
EntityValues
为null
,因为您没有为其分配任何内容
您可以将EntityValues=new List();
添加到构造函数的开头以对其进行初始化。您的对象引用(EntityValues)未设置为实例(new List)