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