C# .NET - Nullreferenceexception was unhandled by user code

Asked By prasanna on 09-Dec-10 05:33 AM
Hi this is my code and am getting error like Nullreferenceexception was unhandled by user code
can u pls help me..by finding out my error

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
using A = Apollo; 


public partial class HrHealthAgency : System.Web.UI.Page
{
    
    protected A.HRHealthAgency OHealthagency;   
  
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
        }
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {

       
            OHealthagency.AddHRHealthAgency(txttitle.Text, txtforename.Text, txtsurname.Text, txtjobtitle.Text, txtcompany.Text, txtaddress.Text, txtaddressline1.Text,
                                            txtaddressline2.Text, txttowncity.Text, Convert.ToInt32(txtpostcode.Text.ToString()), Convert.ToInt32(txtphonenumber.Text.ToString()),
                                            txtcontactemail.Text, txtconfirmemail.Text, txtchooseusername.Text, txtchoosepassword.Text, txtconfirmpassword.Text);
        
       
        Clear();
        
    }
    protected void Clear()
    {
        txttitle.Text ="";
        txtforename.Text="";
        txtsurname.Text="";
        txtjobtitle.Text="";
        txtcompany.Text=""; 
        txtaddress.Text="";
        txtaddressline1.Text="";
        txtaddressline2.Text=""; 
        txttowncity.Text="";
        txtpostcode.Text="";
        txtphonenumber.Text="";
        txtcontactemail.Text="";
        txtconfirmemail.Text="";
        txtchooseusername.Text="";
        txtchoosepassword.Text="";
        txtconfirmpassword.Text="";
    }
   
}

Santhosh N replied to prasanna on 09-Dec-10 05:37 AM
You could actually check the line of the code which is throwing the exception and see what is the problem..
I suspect the problem could be in this line
OHealthagency.AddHRHealthAgency(txttitle.Text, txtforename.Text, txtsurname.Text, txtjobtitle.Text, txtcompany.Text, txtaddress.Text, txtaddressline1.Text,
                                            txtaddressline2.Text, txttowncity.Text, Convert.ToInt32(txtpostcode.Text.ToString()), Convert.ToInt32(txtphonenumber.Text.ToString()),
                                            txtcontactemail.Text, txtconfirmemail.Text, txtchooseusername.Text, txtchoosepassword.Text, txtconfirmpassword.Text);

Where you are trying to convert textbox values to int32 type and when there is no value available it throws this error..
solution: check to have value before usingConvert.ToInt32 on those textboxes
prasanna replied to Santhosh N on 09-Dec-10 05:50 AM
can you explain me clearly...please
Rohan Dave replied to prasanna on 09-Dec-10 05:52 AM
The root cause of error is that you haven't initialized the object of A.HRHealthAgency with " new " keyword. You have just defined the object of A.HRHealthAgency type but not really initialized.

So just make below changes and it will work for you...

protected A.HRHealthAgency OHealthagency = new A.HRHealthAgency ();
prasanna replied to Rohan Dave on 09-Dec-10 06:18 AM

Am getting error like this
does not contain a constructor that takes 0 arguments..
 if i use
protected A.HRHealthAgency OHealthagency = new A.HRHealthAgency ();


Santhosh N replied to prasanna on 09-Dec-10 06:27 AM
check the method in apollo class and the method may be taking extra parameters and you are not passign any parameters
Santhosh N replied to prasanna on 09-Dec-10 06:27 AM
I mean to say constructor :)
Rohan Dave replied to prasanna on 09-Dec-10 08:33 AM
Yes the error is saying that there is not constructor without any parameter.

So do one thing simply define one constructor in your class with no parameters... See the below parameter less constructor:

public A.HRHealthAgency ()
{
}
help
Error follows: [NullReferenceException: Object reference not set to an instance of an object.] TrackChamp.ContestTrackMaint.InitializeComponent() +84 TrackChamp d probably still think you're smart anyway). keywords: NullReferenceExceptionError, Page, EventHandler, EventArgs, Control description: NullReferenceException: at InitializeComponent Error follows: [NullReferenceException: Object reference not set to an instance of an object.] TrackCh
System NullReferenceException' VB.NET 06-Jun-13 10:29 AM An unhandled exception of type 'System.NullReferenceException' occurred in CCFinSol.exe Additional information: Object reference not set to an instance of an mistake it was a variable that was declared but not used in the form. keywords: NullReferenceException, ToShortDateString, DBNull, exception, DateTime, VB.NET, VB, Trans Eff, Eff description: 'System NullReferenceException' An unhandled exception of type 'System.NullReferenceException' occurred in CCFinSol.exe Additional information: Objec
Hi all, I have no idea what NullReferenceException was unhandled means. Could someone explain it to me in general, maybe with a simple point. . . flawed code makes no one happy keywords: NullReferenceExceptionStreamWriter, StreamReader, VB.NET, date, AddMinutes description: NullReferenceException was unhandled Hi all, I have no idea what NullReferenceException was unhandled means. Could someone explain it to me in general
I'm trying to connect Ms Access database with vb.net 2005, but i'm getting an Error saying: "NullReferenceException was Unhandled - Object reference not set to an instance of an object." could someone help I'm a beginner. how to overcome this error?? Anyone can help me? keywords: OleDbCommandBuilderOleDbDataAdapter, NullReferenceException, OleDbConnection, Error, EventArgs description: NullReferenceException was Unhandled I'm trying to connect Ms Access database with vb.net 2005, but i'm getting an Error saying: NullReferenceException w
Hi All, when running the project i am getting this error please do help me. . error is "System.NullReferenceException: Object reference not set to an instance of an object". and my code is string DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("DropDownList1"); string val = ddl.SelectedValue; keywords: GridViewRowEventArgsDataControlRowType, NullReferenceException, DropDownList, Page, ExecuteNonQuery description: Why nullReferenceException error occurs in code Hi All, when i am running the project i am getting
I have bound the text FileRegister.Prefix") When I tried to unbind it as shown below, it gives me System.NullReferenceException was unhandled. Please how can i handle that exception? Thanx in advance, Ben frmBsearch.txtPrefix it can bind, then the form instance would not throw that exception. thanks anyway keywords: NullReferenceException, exception, VB.NET, Remove, Show, instantiated, unhandled description: System.NullReferenceException was unhandled I have bound the text box to the required field as follows:frmBsearch
I want to execute my program by every 5 minutes, But it shown error. . . . . System.NullReferenceException: Object reference not set to an instance of an object. public partial class MainForm : Form how can i rectify for that one. . keywords: NullReferenceExceptionEventHandler, EventArgs, class, Tables, Timer description: System.NullReferenceException: Object reference not set to an instance This s my coding, I want to execute
I want to execute my program by every 5 minutes, But it shown error. . . . . System.NullReferenceException: Object reference not set to an instance of an object. public partial class MainForm : Form InsertintoMysql(); i++; } } } hope this will help you keywords: NullReferenceExceptionEventHandler, EventArgs, class, Tables, Timer description: System.NullReferenceException: Object reference not set to an instance This s my coding, I want to execute
CE Application Compact Framework 06-Jun-13 10:22 AM Hello All, I am facing "NullReferenceException" in compact framework with Win CE application. I have written Try Catch in each block of code, still i get message box of 'NullReferenceException' and it need to restart my application again. I am using one device to run going wrong. And why it is not catching this error. Thanks in advance. Sujit. Hi NullreferenceException is normally happen when you haven't create instance to objects. To solve this follow code in yoru Managed code you need worry about that. private void CatchMe() { try { } catch (NullReferenceException nex) { MessageBox.Show(nex.Message); } } Try above and if you can please post your code on. Thank you http: / / www.codegain.com keywords: Compact Framework Null Reference ExceptionCompact Framework, NullReferenceException, GetLastWin32Error, exception, Application description: Null Reference Exception in WIN CE Application Hello All, I am facing NullReferenceException in compact framework with Win CE application. I have writt