【问题标题】:got the exception Object reference not set to an instance of an object得到异常对象引用未设置为对象的实例
【发布时间】:2012-08-08 10:21:14
【问题描述】:

我开发了一个 Windows 应用程序(C#.net)。我得到了例外,

对象引用未设置为对象的实例。

堆栈跟踪是

在 OnamVideoCable.Bill_Generate.btnBill_Click(对象发送者,EventArgs e) 在 C:\FinalOnam\CableOperatorSoftware\OnamVideoCable\OnamVideoCable\Bill 生成.cs:第 179 行

在 System.Windows.Forms.Control.OnClick(EventArgs e)

在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件)

在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons 按钮,Int32 点击)

在 System.Windows.Forms.Control.WndProc(Message&m)

在 System.Windows.Forms.ButtonBase.WndProc(Message& m)

在 System.Windows.Forms.Button.WndProc(Message&m)

在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)

在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& 味精)

在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 原因, Int32 pvLoopData)

在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文)

在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文)

在 OnamVideoCable.Program.Main() 中 C:\FinalOnam\CableOperatorSoftware\OnamVideoCable\OnamVideoCable\Program.cs:line 19

在 System.AppDomain._nExecuteAssembly(Assembly 程序集,String[] 参数)

在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、Object 状态)

在 System.Threading.ThreadHelper.ThreadStart()

代码:

private void btnBill_Click(object sender, EventArgs e)
{
    int CustNo=0;
    int PayMonth;
    objB.OB = Convert.ToDouble(txtOB.Text);
    
    if (ddCustSelect.Text == "All Customers")
    {
        try
        {
            DataTable dtCustNo = objCust.GetCustInfoForBill();
            foreach (DataRow drCustNo in dtCustNo.Rows)
            {
                 CustNo = Convert.ToInt32(drCustNo["CustNo"]);
                 DataTable dtR = new DataTable();
                    dtR=objR.GetLastReceiptInfo(CustNo);
                if (dtR != null)
                {
                    if (dtR.Rows.Count > 0)
                    {
                        DataRow drR = dtR.Rows[0];
                        //groupBox1.Visible = true;
                        //lblReceipt.Text = Convert.ToString(drR["RNo"]);
                        //lblPaidAmt.Text = Convert.ToString(drR["Amount"]);
                        // lbloutstand.Text = Convert.ToString(drR["Balance"]);
                        //lblMonthRate.Text = Convert.ToString(drR["MonthlyRate"]);
                        DateTime paydate = Convert.ToDateTime(drR["date"]);
                        //lblPayMonth.Text = Convert.ToString(drR["paydate"]);
                        PayMonth = paydate.Month;
                        objB.LastOut = Convert.ToDouble(drR["Balance"]);
                        total = calculate_bill(PayMonth, ddMonth.SelectedIndex + 1, Convert.ToDouble(drR["Balance"]), Convert.ToDouble(drR["MonthlyRate"]));
                    }
                    else
                    {
                        PayMonth = ddMonth.SelectedIndex + 1;
                        DataTable dtRate = objRate.GetExistCustRate(CustNo);
                        DataRow drRate = dtRate.Rows[0];
                        //total = calculate_bill(PayMonth, ddMonth.SelectedIndex + 1, 0, Convert.ToDouble(drRate["Total"]));
                        //objB.LastOut = 0;
                        total = Convert.ToDouble(drRate["Total"]);
                        objB.LastOut = total;
                    }
                }
                else
                {
                    PayMonth = ddMonth.SelectedIndex + 1;
                    DataTable dtRate = objRate.GetExistCustRate(CustNo);
                    DataRow drRate = dtRate.Rows[0];
                    //total = calculate_bill(PayMonth, ddMonth.SelectedIndex + 1, 0, Convert.ToDouble(drRate["Total"]));
                    //objB.LastOut = 0;
                    total = Convert.ToDouble(drRate["Total"]);
                    objB.LastOut = total;
                    //MessageBox.Show("Table is null");
                }
                objB.CustNo = Convert.ToInt32(CustNo);
                objB.Month = ddMonth.Text;
                objB.Year = ddYear.Text;
                objB.Total = total;

                DataTable dtBill = new DataTable(); 
                   dtBill = objB.checkBill();
                   if (dtBill != null)
                   {
                       if (dtBill.Rows.Count > 0)
                       {
                           // MessageBox.Show("Bill is Already Generated");
                           continue;
                       }
                       else
                       {
                           objB.GenerateBill();

                       }
                   }
                   else
                   {
                       //MessageBox.Show("2nd table is null");
                       objB.GenerateBill();
                   }
               
            }
            MessageBox.Show("Bill is generated");
        }
        catch (Exception ex)
        {
           // MessageBox.Show(ex.Message + CustNo);
            throw ex;
        }
    }
    if (ddCustSelect.Text == "Single Customer")
    {
        try
        {
            //panelSingle.Visible = true;
            // groupBox1.Visible = true;
            objB.CustNo = Convert.ToInt32(ddCustNo.Text);
            objB.Month = ddMonth.Text;
            objB.Year = ddYear.Text;
            objB.Total = Convert.ToDouble(lblTotalBill.Text);
            if (lbloutstand.Visible)
            {
                objB.LastOut = Convert.ToDouble(lbloutstand.Text);
            }
            else
            {
                objB.LastOut = objB.Total;
            }
           
            DataTable dtBill = objB.checkBill();
            if (dtBill != null)
            {
                if (dtBill.Rows.Count > 0)
                {
                    MessageBox.Show("Bill is Already Generated");
                }
                else
                {
                    if (objB.GenerateBill())
                    {
                        MessageBox.Show("Bill is generated..");
                    }
                    else
                    {
                        MessageBox.Show("Error while generating bill");
                    }
                }
            }
            else
            {
                MessageBox.Show("2nd table null");
            }
        }
        catch(Exception)
        {
        }

        
    }
}//----this is line no 179

【问题讨论】:

  • 访问时有些内容为空
  • 请向我们展示 btnBill_Click 的代码。
  • 哪个对象抛出异常?尝试初始化该特定对象。

标签: c# .net


【解决方案1】:

因此请仔细查看(或接近 - 数字可能少一些)第 179 行

在 C:\FinalOnam\CableOperatorSoftware\OnamVideoCable\OnamVideoCable\Bill Generate.cs:line 179 中的 OnamVideoCable.Bill_Generate.btnBill_Click(Object sender, EventArgs e)

里面的东西是空的。添加断点并使用调试器。要么检查空值,要么修复任何导致它意外空值的东西。我不能告诉你什么 - 我看不到你的代码!

【讨论】:

  • @sonal 是第 179 行吗?
  • private void btnBill_Click() 的右花括号
  • @sonal 然后(还看到您对调试器的评论)您将需要将此方法分解一下(将其重构为方法)并添加日志记录/某种“我是什么当我爆炸时做”的信息。我们无法为您调试。玩得开心。
  • 你能详细说明一下吗?我有 300 多个 custno,所以完成 foreach 循环需要很长时间。
  • @sonal “详细”?当然:其中一条是做一些得到空值的事情,而不是处理它。只有你能找出哪个。我们不是通灵者。调试它。这并不总是意味着“使用调试器”。这是你工作的基本部分,所以……去做吧。
【解决方案2】:

查看Generate.cs:line 179,检查你在这一行使用的变量,其中一个为null

【讨论】:

  • 在调试项目时我没有得到异常。
  • private void btnBill_Click() 的右花括号
最近更新 更多