簡體   English   中英

得到了異常對象引用未設置為對象的實例

[英]got the exception Object reference not set to an instance of an object

我已經開發了一個Windows應用程序(C#.net)。我得到了例外,對象引用未設置為對象的實例。

堆棧跟蹤為

在OnamVideoCable.Bill_Generate.btnBill_Click(Object sender,EventArgs e)中的C:\\ FinalOnam \\ CableOperatorSoftware \\ OnamVideoCable \\ OnamVideoCable \\ Bill Generate.cs:line 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原因,Int32 pvLoopData)

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

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

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

在System.AppDomain._nExecuteAssembly(程序集程序集,String []參數)

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

在System.Threading.ExecutionContext.Run(ExecutionContext執行上下文,ContextCallback回調,對象狀態)

在System.Threading.ThreadHelper.ThreadStart()

碼:

私人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

因此,請仔細查看(或接近-數字可能會少一些)第179行

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

那里的東西為空。 添加一個斷點並使用調試器。 請檢查是否為空,或者修復使它意外為空的所有內容。 我無法告訴您什么-我看不到您的代碼!

查看Generate.cs:line 179,檢查您在此行中使用的變量,其中之一為null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM