
Welcome! Let's learn principles, features, and techniques to get rid of NullReferenceException. Fire up your Visual Studio or favorite IDE, download the companion source code, and let's get rid of this exception once and for all.
In C#, everything is an object. And there are two types of object references: Value Types and Reference Types. As a prerequisite of understanding NullReferenceException, let's quickly cover the difference between those two types of object references.
Let's write our first code block that throws NullReferenceException. And more important than that, let's cover how not to fix that exception. No, a try/catch is not the solution.
Now it's your turn: play with an array of movies to trigger NullReferenceException.
Pause the video and try to do it yourself. If you're stuck, there are some clues and a solution at the end.
OK, wrapping a code block inside a try/catch is not the right way to fix NullReferenceException. What is it, then? Let's use the CP Technique: checking and preventing for null values.
Now it's your turn: use the CP Technique to fix NullReferenceException from a method that prints a movie report.
Pause the video and try to do it yourself. If you're stuck, there are some clues and a solution at the end.
Let's cover three operators we have to simplify our null checks. These operators have funny names, but it's not that hard to use them.
If the solution to NullReferenceException is checking our references before using them. The real question to answer is: is there anything that could tell us when to check for null? The answer is yes! C# has a feature called: Nullable References.
When we turn on this feature, the compiler tells us code blocks where we might access a reference that could be null. This is a feature you should turn on in your new projects.
Nullable References is great to find code blocks that might throw NullReferenceException. But there's a known edge case when working with arrays and Nullable References.
Even though, we declare an array as non-nullable, if we don't initialize its values, we get null when we try to access them and the compiler doesn't warn us. Bad compiler!
Now it's your turn: let's revisit our last exercise. But this time, let's activate Nullable References and solve it again. The goal? See how easy it's to spot places where we could get NullReferenceException.
Pause the video and try to do it yourself. If you're stuck, there are some clues and a solution at the end.
Functional Programming languages, like F# and Haskell, use a different approach to represent nullable types. They wrap references inside a container or wrapper using an Option type. Well, they call it Maybe. But it's the same idea. Let's learn how to use the Option type to replace null.
Let's continue using the Option type. This time, let's use the utility methods Match and MatchSome.
The best of all C# features is LINQ. With LINQ, we filter, group, and manipulate collections with a more readable syntax than loops, like for and foreach. There's one LINQ method called FirstOrDefault to find the first matching element of a collection. But if we're not careful enough, we could get a NullReferenceException when using it.
Now it's your turn: we have a code block that uses FirstOrDefault and guess what? It throws NullReferenceException. You should fix it.
Pause the video and try to do it yourself. If you're stuck, there are some clues and a solution at the end.
Your last exercise. This exercise is inspired by a real NullReferenceException issue I found on a repository on GitHub. Let's imagine that the Setup page of a web server is breaking. The goal? Troubleshoot and fix it. For this exercise, feel free to use any of the technique we've cover so far.
Pause the video and try to do it yourself. If you're stuck, there are some clues and a solution at the end.
If you have reached this point, congrats. I hope you have learned how to avoid NullReferenceException from your code. No more debugging to inspect our code line by line to fix this exception. Let's summarize what we covered in this course.
"Object reference not set to an instance of an object."
If you have found that scary exception message, you're not alone.
As C# software developers, we all have found it at some point.
I've found it dozens of times while working with past employers and clients on codebases of different sizes and complexities.
And often when we find it, the only thing we could do is fire up the Visual Studio debugger, go through the code line by line, and inspect all parameters and variables until finding the "object reference not set..." Boring and time-consuming. Arrrggg!
That's NullReferenceException in C#.
The "Billion-dollar mistake."
If you want to get rid of this exception from your code, this is the course for you.
Learn principles, features, and strategies to say goodbye to NullReferenceException—in 1 hour and 5 minutes, to be precise.
After finishing this course, you will:
Understand when NullReferenceException is thrown.
Use Nullable Operators to simplify null checks.
Work with Nullable References to identify troublesome code blocks.
Adopt the Option type as an alternative to Nullable References.
Safely use LINQ FirstOrDefault method.
No expertise required to write code free from the NullReferenceException.
You only need to be comfortable declaring and initializing objects and working with conditionals, loops, and collections such as arrays and lists.
See you in the first lesson and let's get rid of this exception once and for all.