![object reference not set to an instance of an object object reference not set to an instance of an object](https://www.ibm.com/support/pages/system/files/support/swg/cogtech.nsf/0/ae78c4b711863fd88525752800296ae2/Symptom/0.84.gif)
- Object reference not set to an instance of an object code#
- Object reference not set to an instance of an object series#
Object reference not set to an instance of an object code#
This will require you to run the code locally, though. When running the code, the debugger breaks on the NullReferenceException and you now see the Exception Thrown window: In this case, you can check System.NullReferenceException. Check Common Language Runtime Exceptions (if not already checked) or extend the node and check the exceptions you are interested in. To enable the analysis go to Debug | Windows | Exception Settings. Let's change the example back to method-chaining: var street = service.GetUser().Address.Street With this in place, Visual Studio can show you exactly what is null. If you are on Visual Studio 2017 or newer (if not, now is the time to upgrade), you will have the Null Reference Analysis feature available. Using Null Reference Analysis in Visual Studio While splitting code into atoms like this can help debug what is going wrong, it's not preferable in terms of readability (IMO). In the example above user.Address returns null, why address.Street causes the NullReferenceException. Running the code reveals the actual call causing the exception: Spotting which call that caused an error is a lot easier if the calls are split into multiple lines: var service = new Service() Splitting chained method-calls to multiple lines ➡️ elmah.io is the easy error monitoring system for. Let's look at the most commonly used ones. There is a range of different ways to find out what is going on. If the code above throws a NullReferenceException, what is null? service? The result of GetUser()? Address? At first glance, Visual Studio isn't exactly helpful either: Take a look at the following example: var street = service.GetUser().Address.Street Sometimes spotting what is null can be hard. When looking at the code above, it's clear that s is null and the stack trace even tells us that: We already know why the exception is happening. System.NullReferenceException: Object reference not set to an instance of an object. Running the code above will produce the following error: Being a plain old C# exception, NullReferenceException can be caught using a try/catch: try
![object reference not set to an instance of an object object reference not set to an instance of an object](https://i.stack.imgur.com/HZ82F.png)
There are some clever ways to avoid a NullReferenceException, but before we start looking into those, let us see how the exception can be caught. The exception happens when you try to invoke a reference that you were expecting to point to an object but in fact, points to null. Today's exception is, without a doubt, the error most people have experienced: System.NullReferenceException.
Object reference not set to an instance of an object series#
Time for another post in the series Debugging common.