Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用列表复选框中的值填充数组_C#_Asp.net_Arrays_Nullreferenceexception - Fatal编程技术网

C# 使用列表复选框中的值填充数组

C# 使用列表复选框中的值填充数组,c#,asp.net,arrays,nullreferenceexception,C#,Asp.net,Arrays,Nullreferenceexception,我试图用复选框中的值填充数组。我正在使用列表复选框。我花了几个小时来找出错误。我正在获取用户代码未处理的NullReferenceException。错误指向代码的i++位。当我注释test[I]=item.Value时;和i++;行我可以警告所选的值,但不能将它们添加到数组中 protected void Button1_Click(object sender, EventArgs e) { string []test=null; int i

我试图用复选框中的值填充数组。我正在使用列表复选框。我花了几个小时来找出错误。我正在获取用户代码未处理的NullReferenceException。错误指向代码的i++位。当我注释test[I]=item.Value时;和i++;行我可以警告所选的值,但不能将它们添加到数组中

protected void Button1_Click(object sender, EventArgs e)
        {

        string []test=null;

        int i = 0;

        foreach (ListItem item in CheckBoxList1.Items)
        {

            if (item.Selected)
            {
                // oneSelected = true;

                test[i]=item.Value;
                i++;

                Response.Write("<script LANGUAGE='JavaScript' >alert('"+item.Value+"')</script>");

            }


        }
     }  
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
字符串[]test=null;
int i=0;
foreach(CheckBoxList1.Items中的ListItem项目)
{
如果(选定项)
{
//oneSelected=true;
试验[i]=项目价值;
i++;
响应。写入(“警报(“+item.Value+”)”);
}
}
}  

您需要实例化数组,但为了实现这一点,您必须有一个大小。我建议改为使用列表

protected void Button1_Click(object sender, EventArgs e)
    {

    var test = new List<string>();

    foreach (ListItem item in CheckBoxList1.Items)
    {

        if (item.Selected)
        {
            // oneSelected = true;

            test.Add(item.Value);

            Response.Write("<script LANGUAGE='JavaScript' >alert('"+item.Value+"')</script>");

        }


    }
 }  
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
var test=新列表();
foreach(CheckBoxList1.Items中的ListItem项目)
{
如果(选定项)
{
//oneSelected=true;
测试.添加(项目.值);
响应。写入(“警报(“+item.Value+”)”);
}
}
}  

几乎所有的
NullReferenceException
情况都是相同的。请参阅“”以获取一些提示。谢谢,它正在工作