C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » General »

Object reference not set to an instance of an object


Posted Date: 24 Jun 2009    Resource Type: Articles    Category: General
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 7



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



For more details, visit http://vijirajkumar.blogspot.com/2009/07/object-reference-not-set-to-instance-of.html



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Object reference not set to an instance of an object  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: About DotnetSpider little Bit
Previous Resource: Upgrading VB6 Projects to VB.NET
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use