object Reference not set to instance of variable

Posted by Sanmukhait.Btech under ASP.NET on 4/19/2012 | Points: 10 | Views : 1156 | Status : [Member] | Replies : 6
What are the different scenario of causing the following error :
object Reference not set to instance of variable
kindly provide code snippet as example.

sanmukh


Responses

Posted by: Sabarimahesh on: 4/19/2012 [Member] Bronze | Points: 25
Posted by: Leila on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
this error appears when you access an object without initiate it. (it si still reference to null)

Example:
public class User0
{
public int ID;

public static User LoadUser(int id)
{
User user =null;
User.ID = id; //<==== Here the error mentioned above appears.
// To resolve the error, yu should replace the above 2 lines by the following
User user = new User();
User.ID = id;
}

}


Thanks,
Leila

Sanmukhait.Btech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sunny4989 on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
1 - You never assigned anything to the variable.
2 - You never created an instance of the value assigned to the variable.
3 - You set the variable equal to Nothing, or a called function did the same thing.


------------------------------------------------
Learn throughout life

Sanmukhait.Btech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/19/2012 [Member] Bronze | Points: 25

Up
0
Down
http://visualbasic.about.com/b/2008/10/19/object-reference-not-set-to-an-instance-of-an-object.htm

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sanmukhait.Btech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/19/2012 [Member] Bronze | Points: 25

Up
0
Down
http://forums.aspfree.com/net-development-11/common-causes-of-object-reference-not-set-to-an-instance-45799.html

read this forums

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Sanmukhait.Btech, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dotnetrajanikanth on: 4/20/2012 [Member] Starter | Points: 25

Up
0
Down
This error will occur in several circumstances.

When you create an object and that object has not been created this error willcome.

you create collections like list,hashtable etc and you are trying to access an unassigned key from that or if you are trying to access an operation like counting the hastable entry then this error will come.

Mainly it comes due to the problem that you have not assigned a value to the object that you have created.

____________
www.flickr.com/photos/psdesigner/

Sanmukhait.Btech, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response