Sunday, July 5, 2009

Object reference not set to an instance of an object

The error Object reference not set to an instance of an object is very common in .NET applications.


In this Article we can see the various possible reasons when the error

Object reference not set to an instance of an object can come and what are the possible Solutions.

Reasons:


1. Trying to access an Object without creating an reference to the Instance.

For example,

trying to access the Methods of a class Without creating the reference to the Class.

Consider the following:


public class A
{
int Var1;
int Var2;

public void SetVariables
{
Var1 = 10;
Var2 = 20;
}

};




To access the method SetVariables From the class B, we need to create the reference to the class.




public class B
{
A objA; //Create Object

.....

No Reference to the Object

//Calling the Method SetVariables

objA.SetVariables(); //Is this Correct?

}





The above will throw error Object reference not set to an instance of an object.



The reason is that only creation of object is done but no reference to the Object.


Solution:

Create reference using New keyword.

Modified Code



public class B
{
A objA; //Create Object

.....

//Reference to the Object

objA = new A();

//Calling the Method SetVariables

objA.SetVariables(); // Correct Calling
}






2. No Initialization of variables


When trying to assign the value of Null value to some variable or trying to compare the Null value the error comes.


For example,



String Myname; //No initialization

.......

if (Myname == "Viji") //Comparing data without initialization
{
// do something

}




In the above example the string Myname is not initialized. But we are comparing the value.


Solution:

Initialize the variables.


Modified Code



String Myname = String.Empty; //proper initialization

.......

if (Myname == "Viji") //Proper Comparison
{
// do something

}






Consider another example,



int i; //No initialization

int j;

.......

j = i * 2; //Assigning null value


Modified Code

int i = 0; //proper initialization

int j = 0;

.......

j = i * 2; //proper value





3. Trying to access Non Scope variable


The variable declaration might belong to another block of code and trying to access from another block of code can cause the error.


For example,




private void doTest()
{

//Block 1
{
int i = 10;
int j = 20;

//do something

}


//Block 2

{
int sum = 0;

sum = i * j; //Trying to access non scope variable


}

}




Solution:

Declare the variables properly and avoid using non scope variables.


Modified Code



private void doTest()
{

int i = 20;
int j = 30;

//Block 1
{

//do something

}


//Block 2

{
int sum = 0;

sum = i * j; //Now the variables are accessible

}

}






4. Forgot to assign name of the controls in ASP.NET


This error comes when we assign the controls in aspx page and try to access it in code-behind class.




< body>

< UserCtl:BtnControl runat="server" / >

< /body >




In Code - behind class




Protected MyControl As MyNamespace.MySite.BtnControl

Sub Page_Load Handles Mybase.Load

MyControl.Text = "Custom button" //Accessing without name of the control

End Sub




This won't work. As the control name is not mentioned, the page cannot identify the control.



Solution:

Assign name or Id to the Control

Modified Code




< body >

< UserCtl:BtnControl Id = "Btn1" runat="server" / >

< /body >





5. Try to access buried Control


Buried Control - If the control is defined with in a template of another control this is called buried Control


Example




< asp:Repeater ID="Repeater1" runat="server" >
< ItemTemplate >
< asp:Label ID="lblID" runat="server" / >
< /ItemTemplate >
< /asp:Repeater >


Private Sub Page_Load(sender As Object, e As EventArgs)

lblID.Text = "This Won't Work" //It will work because the control
//Cannot be accessed

End Sub





Solution:

Using FindControl method find the control and acccess the text

Modified Code




Private Sub Page_Load(sender As Object, e As EventArgs)

Dim lbl As Label

lbl = Repeater1.Controls.(0).FindControl("lblID")

lbl.Text = "this WILL work"

End Sub

No comments:

Post a Comment