未将对象引用设置为对象的实例.-我的复选框数组为空值 [英] Object reference not set to an instance of an object.-i got null value of checkbox array
本文介绍了未将对象引用设置为对象的实例.-我的复选框数组为空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public CheckBox[] cb = null;
int z=0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cb = new CheckBox[count - k];
//Database code.........
while (dr.Read())//read data from access database
{
cb[z] = new CheckBox();
cb[z].Text = dr["Member_Name"].ToString();
Panel2.Controls.Add(cb[z]);
Panel2.Controls.Add(new LiteralControl("</br>"));
z = z + 1;
}
}
}
protected void Button6_Click(object sender, EventArgs e)
{
for (int x = 0; x < cb.Length; x++)//ERROR IS HERE:Object reference not set to an instance of an object.
{
//processing check boxes
}
}
推荐答案
从错误消息中看来,这是:
From the error message it appears that this:
cb = new CheckBox[count - k];
永远不会被打电话.尝试设置断点和/或在下一行显示一条消息,以确保调用了该函数并初始化了数组.
never gets called. Try setting a breakpoint and/or displaying a message in the next line to make sure that the function is called and the array gets initialized.
请在调试时检查Button6_Click(). />
我确定cb [CheckBox]必须为null.那么length属性在那里无效,从而引发错误.
please Check the Button6_Click() while debugging.
I''m sure cb[CheckBox] must be null. then the length property invalid there, so that it throws the error.
这篇关于未将对象引用设置为对象的实例.-我的复选框数组为空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文