【问题标题】:object reference not set to an instance of object exception when I try to add element to an array of type List<string>当我尝试将元素添加到 List<string> 类型的数组时,对象引用未设置为对象异常的实例
【发布时间】:2013-06-24 08:16:59
【问题描述】:

在我的项目中,我有一个变量 numtorust ,它代表旅游的数量。 (用于测试目的 numtourist = 2)

for (i=0,numtoursut; i++)

我动态地创建了 5 个复选框,并为每个游客分配了 checkChanged 事件。另外为了跟踪哪个复选框适用于哪个游客,我添加了属性'collection'

mycheckbox.InputAttributes.Add("collection", i.ToString());

在 checkedchanged 事件处理程序中 - 当用户选择一个复选框时,我检查它的集合属性是 = 0 还是 1(第一个或第二个用户)。然后我将复选框值添加到myche1,如果集合属性 = 1,它的类型为List&lt;string)

但是当我决定创建一个类型为List&lt;string&gt;数组 名称为Toursit 当我尝试向其中添加元素时,出现异常 - 在我的代码的这一行中没有将对象引用设置为到对象的实例

Toursist[Int32.Parse(chk.InputAttributes["collection"])].Add(chk.InputAttributes["value"].ToString());     

这是我的完整代码

 protected void checkChanged(object sender, EventArgs e)
 {
     CheckBox chk = (CheckBox)sender;
     /*that doesn't work

     if (chk.Checked)
     {
         Toursist[Int32.Parse(chk.InputAttributes["collection"])].Add(chk.InputAttributes["value"].ToString());

         ((List<String>[])Session["chk"])[Int32.Parse(chk.InputAttributes["collection"])] = Toursist[Int32.Parse(chk.InputAttributes["collection"])];
     }*/

     //this works with myche1 of type list<string>
     if ((chk.Checked)&&(chk.InputAttributes["collection"].Equals("1")))
     {
         myche1.Add(chk.InputAttributes["value"].ToString());
         lblProba.Text += chk.InputAttributes["value"].ToString();
         Session["chk1"] = myche1;
     }
 }

编辑 1:

新代码

protected void checkChanged(object sender, EventArgs e) {

List<string>[] Toursist = new List<string>[2];
//Session["chk"] = new List<string>[2]; 
for (int i = 0; i < Toursist.Length; i++)
{
    Toursist[i] = new List<string>();
   // ((List<String>[])Session["chk"])[i] = Toursist[i];
}

    CheckBox chk = (CheckBox)sender;
    if (chk.Checked)
    {

      if (((List<String>[])Session["chk"])[Int32.Parse(chk.InputAttributes["collection"])] == null)
        {
            ((List<String>[])Session["chk"])[Int32.Parse(chk.InputAttributes["collection"])] = Toursist[Int32.Parse(chk.InputAttributes["collection"])];


        }
            Toursist[Int32.Parse(chk.InputAttributes["collection"])].Add(chk.InputAttributes["value"].ToString());
            lblProba.Text += chk.InputAttributes["collection"].ToString();
            ((List<String>[])Session["chk"])[Int32.Parse(chk.InputAttributes["collection"])] = Toursist[Int32.Parse(chk.InputAttributes["collection"])];


    }

这次我在测试 if Sessio["chk"] == 0 时再次出现同样的错误。

但是如果我取消注释(所以我不再有这个错误)

// ((List<String>[])Session["chk"])[i] = Toursist[i];

在每个回发事件中,我的会话都是空的,我不想要!!

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您尚未创建任何列表。创建列表数组时,它不会自动创建数组中的所有列表,您必须手动执行:

    List<string>[] Toursist = new List<string>[numtoursut];
    for (int i = 0; i < Toursist.Length; i++) {
      Toursist[i] = new List<string>();
    }
    

    【讨论】:

    • 谢谢!!但是知道当我尝试将 Tourist[i] 存储在这样的会话中时,我遇到了同样的异常 // ((List[])Session["chk"])[Int32.Parse(chk.InputAttributes[ "collection"])] = Toursist[Int32.Parse(chk.InputAttributes["collection"])];
    • @MilkaSalkova:您是否首先将List&lt;string&gt;[] 实例分配给会话变量?
    • 不,但为什么我应该这样做,我只想将 Toursit[i] 存储在 ((List[])Session["chk"])[i] 中。但它不起作用。
    • @MilkaSalkova:您不能在尚不存在的数组中存储任何内容。您必须先将数组放入会话变量中,然后才能在其中存储任何内容。
    • @MilkaSalkova:在创建数组之前,您应该检查会话变量中是否没有数组(即会话变量是否返回null)。如果是这样,创建数组,然后在设置所有项目的循环之后,执行Session["chk"] = Toursist;
    猜你喜欢
    • 1970-01-01
    • 2013-05-01
    • 2012-08-08
    • 2020-11-03
    • 2014-08-22
    • 2011-04-15
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多