请问,我定义一个类,然后定义一个实体类的列表,执行 testList.Add(test);这句的时候总是报错,代码如下:
public class TEST 
{
    public int ScheduleID
    {
        get
        {
            return scheduleID;
        }        set
        {
            scheduleID = value;
        }
    }    public string CreateUser
    {
        get
        {
            return createUser;
        }        set
        {
            createUser = value;
        }
    }    private int scheduleID;
    private string createUser;
}IList<TEST> testList = null;
            TEST test = new TEST();
            test.ScheduleID = 124856;
            test.CreateUser = "user";
            testList.Add(test);

解决方案 »

  1.   

    IList<TEST> testList = null;
    你的 testList  = null?你用它add肯定会报空引用 ,先实例化一个IList<TEST>
      

  2.   

    IList<TEST> testList =new ...
      

  3.   

    List<T>和ArrayList的使用
    using System; 
    using System.Collections; 
    using System.Collections.Generic; public class MyClass 

    public static void Main() 

    List<int> iList = new List<int>(); 
    int[] iArray = new int[0]; 
    ArrayList al = new ArrayList(); 
    int count = 10; 
    //========================== 
    //增加元素 
    //========================== 
    for(int i = 0; i < count; ++i) 

    iList.Add(i); 
    } iArray = new int[count];//需要初始化 
    for(int i = 0; i < count; ++i) 

    iArray[i] = i; 
    } for(int i = 0; i < count; ++i) 

    al.Add(i);//这里有box操作 

    //========================== 
    //输出 
    //========================== 
    foreach(int i in iList) 

    Console.WriteLine(i); 
    } foreach(int i in iArray) 

    Console.WriteLine(i); 
    } foreach(object o in al) 

    Console.WriteLine(o);//这里有unbox操作 

    //============================ 
    //继续增加元素 
    //============================ 
    iList.Add(count); iArray = new int[count+1];//需要重新分配内存 
    for(int i = 0; i < count; ++i) 

    iArray[i] = i; 

    iArray[count] = count; al.Add(count); } }
      

  4.   

    谢谢大家,是我没有实例化:修改为:
    List<TEST> testList = new List<TEST>();
    然后就可以了!