Get tips to solve null reference exception

What Is NullReferenceException? Object reference not set to an instance of an object

Matt Watson Developer Tips, Tricks & Resources

“Object Reference Not Set to an instance of an object.” Cast the first stone those who never struggled with this error message when they were a beginner C#/.NET programmer.

This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a null reference.

But what is a null reference? What are “references” in the first place? How can you stop the NullReferenceException from happening in your code? That’s what we’re going to cover in today’s post.

We’ll start with fundamentals, by giving a brief explanation of what references are in C#/.NET. After that, you’ll learn what null references are. At this point, you’re halfway there to seeing the whole picture.

After this round of theoretical definitions, we’ll get to more practical matters, teaching you how to avoid the NullReferenceException in practice. Let’s dig in.

What Are References?

We already know that the NullReferenceException is caused by a null reference. But what is a null reference? In what way does it differ from a non-null reference?

In .NET, you can divide data types in two categories: value types and reference types. If you have a variable of a value type, it stores the value itself. Reference types variables, on the other hand, don’t hold the value itself. They hold a reference that points to where the object lives in memory.

If it helps you visualize it better, you can think of a reference as a linking pointing to a web page, or a shortcut pointing to a file on your computer. Types such as int (and the other numerical primitive types), DateTime and boolean are value types. That is, structs are value types. Classes are reference types.

So, a reference is what a variable of a reference type contains. These variables can point to “nothing”, though, and that’s what we call a null reference: a reference that doesn’t point to any object. When you try to call a method or another member on the said variable, you got the NullReferenceException.

Understanding the NullReferenceException

Null reference errors are responsible for a good percentage of all application bugs. They are usually very simple problems caused by not adding additional logic to ensure that objects have valid values before using them.  Here are some ways to avoid NullReferenceException.

The following code will throw a NullReferenceException if the variable “text” being passed in is null. You can’t call ToUpper() on a null string.

public void MyMethod(string text)
{
     //Throws exception if text == null
     if (text.ToUpper() == "Hello World")
     {
          //do something
     }
}

You can also have null reference exceptions because any type of object is null. For example, in the code below, the SqlCommand object is never initialized. Not running a SQL query would be a serious problem for your application. A null string might be something you just ignore and move on. Other times, like with the SqlCommand, it could be a fatal issue you don’t want to ignore.

SqlCommand command = null;
//Exception! Object reference not set to an instance of an object
command.ExecuteNonQuery();

Use the Null Conditional Operator to Avoid NullReferenceExceptions

One of the best new additions to C# was the null conditional operator. Instead of having a crazy amount of “variable != null” type checks, you can use the “?” and your code will short circuit and return null instead of throwing the exception. This will make more sense with some examples below:

text?.ToUpper(); //from previous example, would return null

int? length = customerList?.Length; // null if customerList is null   
Customer first = customerList?[0];  // null if customerList is null  
int? count = customerList?[0]?.Orders?.Count();  // null if customerList, the first customer, or Orders is null  

Use Null Coalescing to Avoid NullReferenceExceptions

Another great feature is null coalescing, which is the “??” operator. It works great for providing a default value for a variable that is null. It works with all nullable datatypes.

The following code throws an exception without the null coalescing. Adding “?? new List<string>()” prevents the “Object reference not set to an instance of an object” exception.

List<string> values = null;

foreach (var value in values ?? new List<string>())
{
    Console.WriteLine(value);
}  

Simple Examples of Null Values Causing Problems

Some of the most common causes are settings, database calls, or API type calls not returning expected values. For example, you add a new field to your database and don’t populate default values for every record. Randomly records get queried, and the code didn’t account for that new field being null. KA-BOOM: Object reference not set to an instance of an object.

The Golden Rule of Programming

For years I have had a saying that I say to my team all the time. I call it the golden rule of programming.  I think every new programmer needs a tattoo that says it.

“If it can be null, it will be null”

The good news is that a lot of null reference errors can be avoided by adding additional logic and code to ensure objects are not null before trying to use them. Developers should always assume that everything is invalid and be very defensive in their code. Pretend every database call is going to fail, every field is going to have messed up data in it. Good exception handling best practices are critical.

Tips to Prevent Null Reference Exceptions

  1. Initialize variables with valid values.
  2. If a variable can be null, then check for null and handle it appropriately
  3. Use the ? operator on methods when possible. stringvar?.ToUpper();
  4. Use tools like Resharper to help point out potential null reference exceptions

Avoiding NullReferenceException With C# 8.0’s Nullable Types

One of the main causes of bugs with null reference is the fact that in C every reference type object can be null, all the time. What if you, the developer, had the power to say: “I want this string not to be null, ever”? Better yet, what if this decision was enforced by the compiler itself, preventing you and other developers to assigning null to said variable by accident? Sounds nice? Good news, then: this is a real feature of the eighth version of C# called, unsurprisingly, nullable types.

The feature works in an ingenious and powerful way. It redefines the reference types as being non-nullable by default—as many argue they should’ve been from the start. Then, it adds a new kind of syntax that allows you to define nullable variables (it’s not really new, though, since it’s the same syntax that has been in use for several years for nullable value types.)

To understand better, take a look at the following example:

static int Add(string numbers)
{
    return numbers.Split(",").Select(int.Parse).Sum();
}

In the pre 8.0 version of C#, the code above is dangerous. The numbers variable could be null, which would cause a NullReferenceException when trying to use the Split method.

With C# 8.0 nullable reference types feature, you’d be safe. The variable could never be null and the call to the Split method would never throw. Any attempt of passing null to the Add method would result in a compiling error.

But what if you wanted to allow for null in numbers? In that case, you’d just have to add a question mark after the type’s name:

static int Add(string? numbers)
{
	return numbers.Split(",").Select(int.Parse).Sum();
}

Now things change dramatically. Since numbers can now be null, the compiler will nudge you into checking the variable’s value, with a warning (you could turn the warning into a compiler error, for even more safety):

The compiler is letting me know that “numbers” can be null. Possible solutions include:

  • Using a if-statement to ensure the variable has a valid reference
  • Using the already-mentioned null-coalescing operator when calling the Split method
  • Making the “numbers” variable non-nullable again, by removing the question mark
  • Suppress the rule giving us the warning (which would defeat the whole purpose but hey, it’s an option.)

Keep in mind that this feature is opt-in. That is, it comes disabled by default and you have to activate it in your project’s configuration. The reason for that is that shipping the feature already enabled would cause breaking changes in most code bases.

What Are the Next Steps?

Null reference exceptions are a very common problem in .NET and most programming languages. Luckily, we can all blame Tony Hoare. He invented null references and even calls it the billion dollar mistake.

Jokes apart, how can we avoid such a problem? One alternative is to follow my golden rule: if it can be null, it will be null!

Nowadays, fortunately, we can have the help of the compiler itself when fighting against the NullReferenceException. Enable the “nullable reference types” feature in C# 8.0, and that way you’ll be able to prevent things from being null if you wish so. That allows us a fun spin on my old rule: If it can’t be null, it’ll never be null. The compiler won’t allow it!

Do you want to know more about C#, exceptions, and other related topics? If so, stay in tune with the Stackify blog, since we’re always publishing posts on these topics and more.

Also, make sure you leverage the power of the tools at your disposal. For instance, Stackify offers a tool called Prefix, which allows you to monitor your web application written in .NET or Java. You’ll be able to track the performance of your app and also find hidden exceptions, slow queries, and other problems. Give Prefix a try today.

About Matt Watson

Matt is the Founder & CEO of Stackify. He has been a developer/hacker for over 15 years and loves solving hard problems with code. While working in IT management he realized how much of his time was wasted trying to put out production fires without the right tools. He founded Stackify in 2012 to create an easy to use set of tools for developers.