得到异常对象引用未设置为对象的实例

时间:2012-08-11 08:11:10

标签: c# .net

我开发了一个Windows应用程序(C#.net)。我得到了异常,Object引用未设置为对象的实例。

堆栈跟踪

  

OnamVideoCable.Bill_Generate.btnBill_Click(Object sender,EventArgs)   e)in   C:\ FinalOnam \ CableOperatorSoftware \ OnamVideoCable \ OnamVideoCable \比尔   Generate.cs:第179行

     

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

     

在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

     

在System.Windows.Forms.Control.WmMouseUp(消息& 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&   MSG)

     

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

     

在   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(的Int32   reason,ApplicationContext context)

     

在   System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32   reason,ApplicationContext context)

     

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

     

at System.AppDomain._nExecuteAssembly(Assembly assembly,String []   参数)

     

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

     

在System.Threading.ExecutionContext.Run(ExecutionContext   executionContext,ContextCallback回调,对象状态)

     

在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

2 个答案:

答案 0 :(得分:1)

仔细看看(或接近 - 数字可以是几个)

中的第179行
  

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

那里的东西是空的。添加断点并使用调试器。要么检查null,要么修复任何使其意外为空的内容。我不能告诉你什么 - 我看不到你的代码!

答案 1 :(得分:0)

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