sonal
sonal

Reputation: 279

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

I have developed one windows application(C#.net). I got the exception,

Object reference not set to an instance of an object.

stack trace is

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

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

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

at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

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

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

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

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

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

at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

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

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

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

at OnamVideoCable.Program.Main() in C:\FinalOnam\CableOperatorSoftware\OnamVideoCable\OnamVideoCable\Program.cs:line 19

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

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

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at System.Threading.ThreadHelper.ThreadStart()

code:

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

Upvotes: 1

Views: 2015

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062685

So look carefully at (or near - the numbers can be a few out) line 179 in

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

Something in there is null. Add a breakpoint and use the debugger. Either check for the null, or fix whatever is making it null unexpectedly. I can't tell you what - I can't see your code!

Upvotes: 1

Chen Kinnrot
Chen Kinnrot

Reputation: 21015

look at Generate.cs:line 179, check the variables you use in this line, one of them is null

Upvotes: 0

Related Questions