From the course: Debugging in C#

NullReferenceException

From the course: Debugging in C#

Start my 1-month free trial

NullReferenceException

- [Instructor] One of the most common errors in C#, as well as in other programming languages, is the null reference exception or the null reference error. This error occurs during the program's execution, when the program tries to operate on an object that doesn't exist. When I say operate, I mean the program tries to access the components of the null object. If the object doesn't exist, then the components of the object also don't exist. Let's take a look at some code. In this program, we have a string x, which would give the value null, and then we try to trim x. Here, since x is null, when we try to trim or operate on this null object, we'll get an error. So, let me go ahead and run this and you'll see we'll get this error. This is because we can't trim something that is null. We can't operate on something that doesn't exist. This means that almost any method that we use on this string will error with this null reference error because the string doesn't exist. Let's stop this program and try a different method. If we use index of anything, so let's try a, so try using this method on our null object. We'll run, and we still get this null reference exception error. This is because both of these methods try to operate on the null string, or access components of the null string, and that's what gives us this error. Now, let's take a look at a more ambiguous example. Here, we have a person class where each person has a name that is a string. In the main method, we create a people array and allocate space for 10 persons or 10 people. Then, we try to access the first person's name at index zero. Let's run this and see what happens. In this case, we still get this null reference exception error. Why does this error? Well, just because we allocate space for 10 person objects doesn't mean we actually have 10 person objects in the array. When we access people at zero, the object we get back is null, because we never initialized the person at index zero of the people array. This means when we access the main property of this null object, we get at error. So, let's go ahead and take out the name here and stop the program from running, and we'll add a break point so we can actually see what's inside of this person array before we run this print statement so that you can see inside of this person array of people it's all null. So, we have the person at index zero is null, all of these are null values, and so whenever we try to operate on any of these null values, we're going to get that null reference exception error. Let's go ahead and comment this out and try a similar operation but with a different data type. So, we'll comment that out and we'll create and int ray which we'll call numbers, and it will be a new int ray with 10 items in it. Now, let's try this same line, console dot write line numbers at zero, so here we'll access the item at index zero of our numbers array. We'll add a break point here and just see what's inside of this array when we initialize it. So we'll hit that play button. And we'll actually just step over this so we can get to this next line and see that the number array has items in it. So, here we actually initialized each item in this array to be zero. This didn't happen with our person array because person was a custom object, but int is a value type or a primitive type, a basic type, so when we create an array of ints, each item in the array is automatically initialized with zero. Since int is a built-in type, when we create an array of ints, we automatically get these zeros. Let's continue the program, and at the bottom we see zero is at index zero of the numbers array. Ultimately, this means that when we deal with custom objects, we have to be more mindful of initialization, manipulating values and customization in order to avoid null reference exception errors.

Contents