Object reference not set to an instance of an object

by Cliff 3. July 2011

The infomous Object reference not set to an instance of an object error occurs when you try to reference or use an object that has not been set to a valid "value" or instance.  For example, let's assume you have a Class "Person" that has a property Name.  If you try to access the Name property and your Person object is not set to an instance, you will receive the Object reference not set to an instance of an object error.

In code:

Person p = null;
p.Name = "John Doe";

This is an obvious case, but what makes this error difficult to track down at times is when you do not know why you do not have an instance of the object.  This can occur for a variety of reasons.  If you are able to create a unit test (or already have one) this can help you to track down the root cause.  You can also rely on the stack trace and/or debug the offending code.

Related posts

Comments are closed

Cliff Gray's Info

Cliff Gray
Developer/Founder GrayTechnology.com.

E-mail me Send mail

Authors

Calendar

<<  June 2017  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar

Blogroll

Download BlogEngine.NET

Download at CodePlex

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Subscribe