DEV Community

Cover image for Easy Way to Debug .NET Application Errors
Developer Partners
Developer Partners

Posted on • Updated on • Originally published at developerpartners.com

Easy Way to Debug .NET Application Errors

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.

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

I usually read the 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.

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

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

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.

Top comments (0)