Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Null represents the absence of a value or object.
-
0:00
Let's go back to main to test out the GetLocationAt method.
-
0:05
Let's create a new path object here,
-
0:10
say Path path = new Path.
-
0:14
We can actually have the array created and
-
0:16
pass it into the constructor in a single statement.
-
0:19
Like this, so we'll copy this.
-
0:24
Move it into here.
-
0:28
Then we can say new array and
-
0:33
then, indent this like that.
-
0:40
Now the array is being created and
-
0:41
immediately being passed into the constructor of the path class.
-
0:46
See how that works?
-
0:48
Now, we can use our new GetLocationAt method
-
0:51
to find the location of a step on the path.
-
0:55
So, we'll call GetLocationAt and assign it to a MapLocation variable called location.
-
1:07
Now let's print out the x and y values of this step that we know is on the path.
-
1:12
So we'll say,
-
1:15
Console.WriteLine(location.X + ","
-
1:24
+ location.Y).
-
1:28
Let's compile and run this and see what we get.
-
1:43
All right, we get the first location in the path array.
-
1:47
Now, let's see what happens when we pass in a step that isn't on the path.
-
1:51
The path only has indexes 0 through 7.
-
1:55
So if we put 8 here, this should be one step past the end of the path.
-
2:01
Let's compile and run again.
-
2:04
We see unhandled exception printed to the console.
-
2:09
That doesn't tell us much.
-
2:11
Let's get some more information about what happened.
-
2:14
We're catching every exception that could possibly be thrown.
-
2:18
We catch it in one of these three catch clauses.
-
2:21
We saw unhandled exception printed to the console.
-
2:25
So it looks like the one that caught the exception is this final
-
2:28
catch all we have here.
-
2:31
Let's have this print out some more detailed information
-
2:34
about the exception it caught.
-
2:36
First, we'll need to add an exception variable here.
-
2:39
Now we can just append this variable to the end of our message.
-
2:43
Remember, the string concatenation operator here attempts to convert anything
-
2:48
that isn't a string to a string before the concatenation.
-
2:52
It just so happens that the string that exceptions are converted into
-
2:56
have a lot of good information in them.
-
2:59
We'll learn exactly how this works in a later course.
-
3:02
Now let's run our code again to see what we get.
-
3:08
This is what we get when the exception is converted to a string.
-
3:11
As you can see, the name of the exception thrown is System.NullReferenceException.
-
3:17
This exception happens when we try to use null like a normal object.
-
3:22
Look up here.
-
3:23
GetLocationAt is supposed to return null if we ask for
-
3:27
a step that isn't on the path.
-
3:29
That means that the location variable here is now null.
-
3:35
Now when we tried to get the X field from the location object,
-
3:38
it can't do what we asked, because it isn't the location.
-
3:42
This is null, and null is the absence of a location.
-
3:47
This causes the null reference exception to be thrown.
-
3:51
This is a very common exception to deal with.
-
3:54
So, what should we do to make this code work?
-
3:57
One thing we could do is check to see if location is null,
-
4:01
before we try to print out x and y like this.
-
4:04
So we can say, if(location
-
4:10
!= null then print this to the console.
-
4:15
So if location is null, nothing will get printed and
-
4:20
we won't be trying to access location.X or location.Y.
-
4:26
This is a very common pattern when dealing with null.
-
4:29
It's often called a null check.
-
4:32
There are some interesting links in the teacher's notes if you want to dive deeper
-
4:36
into how to handle null values.
-
4:39
We'll learn other ways to deal with null in future courses,
-
4:42
including how to avoid null entirely.
-
4:45
A good practice when working with objects is to always consider what would happen
-
4:49
if an object is null, and take the proper precautions.
-
4:53
We've also just learned about arrays.
-
4:55
Arrays are pretty handy, are they not?
-
4:57
I mentioned before that arrays are just one of the many ways
-
5:01
to store a collection of objects.
-
5:03
We'll learn about those in other courses.
-
5:05
So in the future,
-
5:07
we might want to change our path class to use one of those instead of an array.
-
5:12
We were smart and used encapsulation to hide the fact that we're using an array.
-
5:17
We could change it from an array to any other type of collection.
-
5:20
And it won't break any code that's using the public parts of the path class,
-
5:24
because those parts won't change.
-
5:26
Next we'll learn about properties.
-
5:28
They're an important part of C# that helps with encapsulation.
-
5:32
I can't wait.
You need to sign up for Treehouse in order to download course files.
Sign up