ASP.net中的未将对象引用设置到对象的实例错误怎么解决

来源:百度知道 编辑:UC知道 时间:2024/06/16 18:52:44
“/”应用程序中的服务器错误。
-------------------------------------------------------------------------------
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 275:
行 276: CheckBox cb2 = (CheckBox)gvList.Rows[i - 1].FindControl("CheckBox_ID");
行 277: int ID = int.Parse(cb2.Text);
行 278: Response.Write(ID+",tttttt<br>");
行 279: int CompanyId = int.Parse(gvList.Rows[i - 1].Cells[1].Text);

源文件: 行: 277

堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]

******************************************
下面是我写的方法循环:

for (int i = 0; i < gvList.Rows.Count; i++)
{
CheckBox cb = (CheckBox)gvList.Rows[i].FindControl("CheckBox_ID");
if (cb.Checked)

不太清楚你想实现什么功能。。不过你似乎复杂了些。。

解决为空的问题就是
逐步判断
if(xxx != null)


GridViewRow gvr=gvList.Rows[i-1];
if(gvr != null)
{
CheckBox cb2=(CheckBox)gvr.FindControl("CheckBox_ID");

}

不明白再问

CheckBox cb2 = (CheckBox)gvList.Rows[i - 1].FindControl("CheckBox_ID");
这句话里面没找到"CheckBox_ID"这个控件

对象是空的

CheckBox cb2 = (CheckBox)gvList.Rows[i - 1].FindControl("CheckBox_ID");
跟踪下 cb2 是什么? 估计是null

断点查看cb是否为null

gvList里有空值!绝对的!