C# RESTful-WCF-对象引用未设置为对象的实例

C# RESTful-WCF-对象引用未设置为对象的实例,c#,wcf,rest,C#,Wcf,Rest,我在WCF(c#)中创建了一个简单的RESTful应用程序。当我使用(GET)填充时,我收到了此错误 “对象引用未设置为对象的实例” 我在target.DocumentLines[0].itemCode=“”)的部分收到错误 这是我的密码: public PRRequestData[] getAllPR() { List<PRRequestData> list = new List<PRRequestData>();

我在WCF(c#)中创建了一个简单的RESTful应用程序。当我使用(GET)填充时,我收到了此错误

“对象引用未设置为对象的实例”

我在
target.DocumentLines[0].itemCode=“”)的部分收到错误

这是我的密码:

        public PRRequestData[] getAllPR()
    {
        List<PRRequestData> list = new List<PRRequestData>();

        try
        {
            string sqlSelect = "SELECT DocEntry, Comments, ReqDate FROM OPRQ";
            APP.strCommand = sqlSelect;
            DataTable dt = new DataTable();

            dt = APP.Ds.Tables[0];

            foreach (DataRow row in dt.Rows)
            {
                // Person target = Activator.CreateInstance();
                PRRequestData target = new PRRequestData();
                target.requiredDate = row["ReqDate"].ToString();
                target.remarks = row["Comments"].ToString();
                target.docEntry = row["DocEntry"].ToString();
                // DataColumnAttribute.Bind(row,target);

                sqlSelect = "SELECT ItemCode, Quantity, Price, VendorNum, TaxCode FROM PRQ1 WHERE DocEntry = '" + row["DocEntry"].ToString() + "' ";
                APP.strCommand = sqlSelect;
                for (var i = 0; i < APP.Ds.Tables[0].Rows.Count; i++)
                {
                    target.DocumentLines[0].itemCode = "";

                }

                list.Add(target);
            }

            return list.ToArray();
        }
        catch (Exception e)
        {
            e.ToString();
        }

        return list.ToArray();

看起来你没有初始化这个属性

[DataMember]
public RequestDataDetails[] DocumentLines;
我建议您使用List而不是RequestDataDetails[],因为无论如何,您都必须使用内部列表

在for循环之前,初始化RequestDataDetails列表以从查询中填充它

List<RequestDataDetails> requestDetails = new List<RequestDataDetails>
而不是

target.DocumentLines[0].itemCode = "";
然后在for循环之后,将列表转换为数组,并将其分配给
target

target.DocumentLines = requestDetails.ToArray();

希望它能起作用

看起来target.DocumentLines[0]或target.DocumentLines为空,是否尝试过调试?是,已尝试过。我要做的是将数据设置为DocumentLines的DataMember。
target.DocumentLines[0].itemCode = "";
target.DocumentLines = requestDetails.ToArray();