“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.
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.
<< Null reference errors are responsible for a good percentage of all application bugs.>>
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”)
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
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?; // null if customerList is null
int? count = customerList??.Orders?.Count(); // null if customerList, the first customer, or Orders is null
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 data types.
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>())
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 is null. KA-BOOM: Object reference not set to an instance of an object.
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.
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
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 from 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.>>
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)
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)
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:
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.
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 blogs, since we’re always publishing posts on these topics and more.
Before you push your code, you must improve the user experience and optimize the bottlenecks. To make this possible, make sure you leverage the power of the tools at your disposal. Take Stackify tools for instance. Stackify offers a tool called Prefix, which allows you to monitor your web application written in .NET, Java, PHP, Node.js, Python, or Ruby.
Prefix is a very lightweight code profiler that can help even the most experienced developers in discovering slow SQL queries and even hidden exceptions. With Prefix, developers can validate the performance of their code as it is written. As a result, they push better code to test and receive fewer support tickets.
Stackify recently introduced Prefix v4 which is still currently in beta. This version is an improved rebuild of the tried and tested profiling tool from Stackify.
With this new and improved Prefix, developers receive more OS support, more programming languages, modern .NET profiling, dedicated log viewer, and Prefix is now a native application for Windows and macOS.
Not only that. While you are running your applications, Prefix works its magic in the background providing detailed snapshots of all the web requests.With Stackify Prefix, you’ll be able to track the performance of your app and also find hidden exceptions, slow queries, and other problems. Download Prefix today.
If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]