【发布时间】:2015-05-13 21:00:37
【问题描述】:
我正在尝试使用复选框中的值填充数组。我正在使用列表复选框。我花了几个小时才找到错误。我得到 NullReferenceException 未被用户代码处理。错误指向 i++ 位代码。当我评论 test[i]=item.Value;和我++;行我可以提醒选择的值,但我不能将它们添加到数组中。
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>");
}
}
}
【问题讨论】:
-
NullReferenceException的几乎所有情况都是相同的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。
标签: c# asp.net arrays nullreferenceexception