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.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89
  • 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? – JMK Mar 20 '13 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. – Federico Berasategui Mar 20 '13 at 22:46
  • Ha ha brilliant @HighCore – Lews Therin Mar 20 '13 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 – Nnp Mar 20 '13 at 22:48

3 Answers3

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.");
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • ok - thats brilliant - so this error is always caused by something being null? - is that correct? – Trevor Daniel Mar 20 '13 at 23:07
  • @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`. – TheEvilPenguin Mar 20 '13 at 23:10
  • many thanks - at least i will be able to find the error quicker now.. very much appreciated – Trevor Daniel Mar 20 '13 at 23:18
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.

System Down
  • 6,192
  • 1
  • 30
  • 34
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.

Community
  • 1
  • 1
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157