对象引用未设置为DataTable.Rows.Add中对象的实例

时间:2019-03-08 14:38:59

标签: c# asp.net datatable

所以我有一个数组和一个数据表

string[] a = new string[6];
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[7] { new DataColumn("name"), new DataColumn("description"), new DataColumn("price"), new DataColumn("quantity"), new DataColumn("images"), new DataColumn("id"), new DataColumn("product_id") });

im将这些值存储在cookie中,并使用以下代码对其进行分割:

if (Request.Cookies["test"] != null)
    {
        s = Convert.ToString(Request.Cookies["test"].Value);
        string[] strarr = s.Split('|');
        for (int i = 0; i < strarr.Length; i++)
        {
            t = Convert.ToString(strarr[i].ToString());
            string[] strarr1 = t.Split(',');
            for (int j = 0; j < strarr1.Length; j++)
            {
                a[j] = strarr1[j].ToString();
            }
            dt.Rows.Add(a[0].ToString(), a[1].ToString(), a[2].ToString(), a[3].ToString(), a[4].ToString(), i.ToString(), a[5].ToString());
        }
    }

运行此页面时出现以下错误:

Object reference not set to an instance of an object. 
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 



Line 29:                     a[j] = strarr1[j].ToString();
Line 30:                 }
Line 31:                 dt.Rows.Add(a[0].ToString(), a[1].ToString(), a   [2].ToString(), a[3].ToString(), a[4].ToString(), i.ToString(), a[5].ToString());
Line 32:             }
Line 33:         }

请帮助。

0 个答案:

没有答案