Easy Way to Debug .NET Application Errors
Tags: csharp, programming, dotnet, testing
Published: 2023-08-10 20:48:29

All applications have bugs at some point. Even solutions built by large companies with a lot of experienced developers may have bugs in them. Having bugs in a program is not the end of the world, but it's important to know how to debug them.
For the sake of simplicity, all the examples in this article will be for a console application. The type of framework we use is not very important for the purposes of this article, so we will use a console application for our examples to show you only the relevant code. We only need to understand the basics of the .NET exceptions and how to find and fix them.

## A Simple Problem
Let's start from a simple problem, then try to fix it. Let's say we are calling the "DoSomething" method from the "Main" method of our console application, and the "DoSomething" method throws an error. Here is the code of the "DoSomething" and "Main" methods:

![NullReferenceException: Object reference not set to an instance of an object.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18jai3kdus9ee7wd6p5j.png)

Here is the exception that we get when we run our console application:
**_Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at HowToDebug.Program.DoSomething(Car myCar) in C:\HowToDebug\Program.cs:line 16
   at HowToDebug.Program.Main(String[] args) in C:\Blog\HowToDebug\Program.cs:line 11_**

An exception has 2 parts: an error message and stack trace. The error message part is the following:
**_Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object._**

The stack trace part of the exception is the following:
 _**at HowToDebug.Program.DoSomething(Car myCar) in C:\HowToDebug\Program.cs:line 16
   at HowToDebug.Program.Main(String[] args) in C:\Blog\HowToDebug\Program.cs:line 11**_

The error message of an exception explains what went wrong. The stack trace shows you where exactly that error happened: what file, what method in that file, and what line number. Knowing what an the error message and stack trace are there for, we can use 4 simple steps for troubleshooting this error.

## 1. Examine the Error Message
As mentioned before, the error message explains what went wrong. Our error message is the following:
_**Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.**_

Some error messages have confusing texts, so it may not be clear what exactly went wrong. This is a fairly common error, so many readers may know what it means. However, if you are not familiar with it, you can Google it and find out. A quick Google search returns quite a few results for this error:

![Google search for "Object reference not set to an instance of an object" .NET exception](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e2b5c4a6iltclen41a8.png)

I usually read the [stackoverflow.com](https://stackoverflow.com/) results because that way it is easy to find an answer quickly. Their answers are short and to the point. It's always good to try to find an answer quickly rather than reading lengthy articles in different programming blogs.
In short, the _NullReferenceException _means that there is a variable or property that is null (not instantiated), and your code is trying to read or assign one of its property values or calling a function on it. So in our simple example, the "myCar" variable is null, and we are trying to call the "Drive" function on it.

## 2. Examine the Stack Trace
Now that we know what went wrong from reading the exception message, we have to find where we got that error (if we don't already know that). In our simple example, it's pretty clear where we got that error. However, in real life scenarios, the solution may have tens of thousands of lines of code, so it may be much harder to know where the error happened with just looking at the error message. In such cases, reading the stack trace of the exception will show you exactly where that error was raised. 

Our stack trace was the following:
at HowToDebug.Program.DoSomething(Car myCar) in C:\HowToDebug\\**Program.cs:line 16**
   at HowToDebug.Program.Main(String[] args) in C:\Blog\HowToDebug\Program.cs:line 11

The text in bold (highlighted by me) shows that the error was raised in Program.cs file on line 16. This is the line 16 from our Program.cs file:

![NullReferenceException: Object reference not set to an instance of an object.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5s1htnnr0ch7lrcgv62.png)


## 3. Put a Breakpoint
Now that we know what went wrong and where exactly that happened, we have to understand why that happened and what we should do to fix it. We know what line that error happened, so we can just put a breakpoint right before that line and examine all our variables and method arguments. Let's put a breakpoint on line 15, just before the line where the error was raised. When our breakpoint hits, we can hover our mouse cursor over the "myCar" method argument and see what its value is:

![Breakpoint right before the line that raised the error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfs7oixorwckg1bc5suc.png)

As you can see from the screenshot above the "myCar" argument value is null. Since our _NullReferenceException_ exception means that we are calling a function on an object that is null, we can tell that the issue is that the "myCar" argument is null, and we are calling the "Drive" function on it that is why we are getting an error.

## 4. Fix the Problem
Next, we should understand why the "myCar" argument value is null. To do that, we have to look at where that value comes from. Since "myCar" is a "DoSomething" method argument, its value is passed when the "DoSomething" method is called. We call the "DoSomething" method on line 11, so we have to fix the issue on line 11. We have to update the code there to pass something other than null to our "DoSomething" method. Here is an updated code that fixed our problem:

![Solution to system.NullReferenceException: Object reference not set to an instance of an object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqrzkg84az70ofwd4zjy.png)

In the screenshot above, we create a new instance of the Car class and store that instance in the "myCar" variable. Then, we pass the "myCar" variable to the "DoSomething" method. That way the value of the "myCar" argument of the "DoSomething" method is not null anymore because we pass a non null value to it.

## Conclusion
It's okay to have bugs in your code. Unfortunately, even software developers with many years of experience make mistakes. However, it is important to know how to troubleshoot those bugs. One easy way of troubleshooting most bugs is following our simple 4 steps:

1. Examine the Error Message.
2. Examine the Stack Trace.
3. Put a Breakpoint.
4. Fix the Problem.

The steps listed above are not the only good way to find and fix bugs, but it's a great way to fix them if you already have the error message. If you don't have an error message for the bug that you are working on, then that might be a logical error. In that case, the troubleshooting steps will be different. Please consider subscribing to our newsletter for being notified when we publish an article about troubleshooting logical errors.