2

I keep hitting the same problem over and over again, and I did it again just now. I wrote a line of code:

int LAID = db.GetLAByLatLong(address.Latitude, address.Longitude);

...and Visual Studio in reports no problem with the line whatsoever.

But when I run the code it reports:

Object reference not set to an instance of an object

What am i doing wrong? How come Visual Studio seems to say the code is fine, but at runtime it reports an error? I seem to be doing this a lot and I would really love to understand what I am doing wrong so I can avoid it.

4
  • It would probably help if you could post a bit more code, can you edit your post with the entire method this line appears in? Commented Mar 20, 2013 at 22:46
  • 5
    int LAID... EPIC... nah, seriously, that error means that something that is expected to be there actually isn't. Where is db coming from? you have to make sure db is not null. Commented Mar 20, 2013 at 22:46
  • Ha ha brilliant @HighCore Commented Mar 20, 2013 at 22:47
  • 1
    VS gives only compile time error, not runtime. syntax wise your code is fine. it is just that one of the oject (db or address) is null Commented Mar 20, 2013 at 22:48

3 Answers 3

8

You have two objects in your code:

db

and

address

You are referencing both objects in the code shown. At least one of them is null.

To avoid that problem, ensure that you have initialized both objects before the code runs. You can also add checks such as

if (db == null) throw new Exception("The variable db is null.");
Sign up to request clarification or add additional context in comments.

3 Comments

ok - thats brilliant - so this error is always caused by something being null? - is that correct?
@TrevorDaniel Correct. db and address are object references, and they can either point to an instance of an object or null. If it's not set to an instance of an object, it must be null.
many thanks - at least i will be able to find the error quicker now.. very much appreciated
3

The code compiles because for all intents and purposes it is correct.

However, that doesn't mean that it cannot cause errors in runtime. The error "Object reference not set to an instance of an object?" means that an object that you are using does not exist. In this line it could be the objects referenced by the variables db or address.

To know which, you'll have to debug the code. Put a breakpoint on that line (click on the space to the left of the line) and press F5. The code will run and then stop at that line, where you can inspect what all the variables contain.

Comments

2

You have to check which variables might be null. See this answer for a list of steps that help you work it out.

In this case one of the db and address variables might be null, and that is the most common cause for a NullReferenceException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.