未将对象引用设置到对象的实例"异常的原因

未将对象引用设置到对象的实例。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:

行 70:             int gridviewQuantity = Convert.ToInt16(GridViewShoppingCartList.DataKeys[i].Value);//int.Parse(GridViewShoppingCartList.Rows[i].Cells[3].Text);
行 71:                 //if textboxquantity is changed and checkedRemove is checked!
行 72:             if( quantity!=gridviewQuantity||checkBoxRemove.Checked==true)
行 73:             {
行 74:                 Label labelBookId = (Label)GridViewShoppingCartList.Rows[i].FindControl("LabelBookId");


源文件: f:/MyOnNetBookSell/ShoppingCarts.aspx.cs    行: 72

以下是代码:

    protected void UpdateShoppingCartList()
    {
        ShoppingCart cart = new ShoppingCart();
        //obtain current user's shopping cart id
        string cartId = cart.GetShoppingCartID();
        //itarate through all rows with the shopping cart
        for (int i = 0; i < GridViewShoppingCartList.Rows.Count;i++ )
        {
            //
            TextBox textBoxQuantity = (TextBox)GridViewShoppingCartList.Rows[i].FindControl("TextBoxQuantity");

            CheckBox checkBoxRemove = (CheckBox)GridViewShoppingCartList.Rows[i].FindControl("TextBoxRemove"); //checkBoxRemove为null值,因为不存在TextBoxRemove控件
            int quantity;
            //try
            //{
            quantity = int.Parse(textBoxQuantity.Text);
            int gridviewQuantity = Convert.ToInt16(GridViewShoppingCartList.DataKeys[i].Value);//int.Parse(GridViewShoppingCartList.Rows[i].Cells[3].Text);
                //if textboxquantity is changed and checkedRemove is checked!
            if( quantity!=gridviewQuantity||checkBoxRemove.Checked==true)//运行到这里就出错
            {
                Label labelBookId = (Label)GridViewShoppingCartList.Rows[i].FindControl("LabelBookId");
                if (quantity == 0 || checkBoxRemove.Checked == true)
                {
                    cart.RemoveItems(cartId,Int32.Parse(labelBookId.Text));
                }
                else
                {
                    cart.UpdateItems(cartId,Int32.Parse(labelBookId.Text),quantity);
                }
            }
            //}
            //catch
          
            //{
            //   LabelError.Visible = true;
            //  LabelError.Text = "There has been a problem with one or more of your inputs.";
            //}

        }
  
    }

出错原因:

在aspx页面中并没有id为TextBoxRemove(原文中只有id为CheckBoxRemove的控件),这就造成了 CheckBox checkBoxRemove = (CheckBox)GridViewShoppingCartList.Rows[i].FindControl("TextBoxRemove");中checkBoxRemove的值为null,因此程序运行到此处时就会报:未将对象引用设置到对象的实例.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值