1 00:00:00,830 --> 00:00:03,997 Let's go back to main to test out the GetLocationAt method. 2 00:00:05,380 --> 00:00:10,579 Let's create a new path object here, 3 00:00:10,579 --> 00:00:14,170 say Path path = new Path. 4 00:00:14,170 --> 00:00:16,050 We can actually have the array created and 5 00:00:16,050 --> 00:00:19,320 pass it into the constructor in a single statement. 6 00:00:19,320 --> 00:00:21,630 Like this, so we'll copy this. 7 00:00:24,491 --> 00:00:25,510 Move it into here. 8 00:00:28,460 --> 00:00:33,920 Then we can say new array and 9 00:00:33,920 --> 00:00:38,410 then, indent this like that. 10 00:00:40,190 --> 00:00:41,920 Now the array is being created and 11 00:00:41,920 --> 00:00:45,130 immediately being passed into the constructor of the path class. 12 00:00:46,440 --> 00:00:47,170 See how that works? 13 00:00:48,500 --> 00:00:51,550 Now, we can use our new GetLocationAt method 14 00:00:51,550 --> 00:00:53,700 to find the location of a step on the path. 15 00:00:55,220 --> 00:01:01,870 So, we'll call GetLocationAt and assign it to a MapLocation variable called location. 16 00:01:07,310 --> 00:01:12,480 Now let's print out the x and y values of this step that we know is on the path. 17 00:01:12,480 --> 00:01:15,400 So we'll say, 18 00:01:15,400 --> 00:01:22,815 Console.WriteLine(location.X + "," 19 00:01:24,162 --> 00:01:28,010 + location.Y). 20 00:01:28,010 --> 00:01:29,779 Let's compile and run this and see what we get. 21 00:01:43,312 --> 00:01:46,650 All right, we get the first location in the path array. 22 00:01:47,780 --> 00:01:51,890 Now, let's see what happens when we pass in a step that isn't on the path. 23 00:01:51,890 --> 00:01:55,050 The path only has indexes 0 through 7. 24 00:01:55,050 --> 00:02:00,180 So if we put 8 here, this should be one step past the end of the path. 25 00:02:01,680 --> 00:02:02,760 Let's compile and run again. 26 00:02:04,720 --> 00:02:07,660 We see unhandled exception printed to the console. 27 00:02:09,910 --> 00:02:11,730 That doesn't tell us much. 28 00:02:11,730 --> 00:02:14,610 Let's get some more information about what happened. 29 00:02:14,610 --> 00:02:18,480 We're catching every exception that could possibly be thrown. 30 00:02:18,480 --> 00:02:21,740 We catch it in one of these three catch clauses. 31 00:02:21,740 --> 00:02:25,130 We saw unhandled exception printed to the console. 32 00:02:25,130 --> 00:02:28,880 So it looks like the one that caught the exception is this final 33 00:02:28,880 --> 00:02:29,980 catch all we have here. 34 00:02:31,360 --> 00:02:34,230 Let's have this print out some more detailed information 35 00:02:34,230 --> 00:02:36,290 about the exception it caught. 36 00:02:36,290 --> 00:02:39,740 First, we'll need to add an exception variable here. 37 00:02:39,740 --> 00:02:43,760 Now we can just append this variable to the end of our message. 38 00:02:43,760 --> 00:02:48,040 Remember, the string concatenation operator here attempts to convert anything 39 00:02:48,040 --> 00:02:51,690 that isn't a string to a string before the concatenation. 40 00:02:52,750 --> 00:02:56,940 It just so happens that the string that exceptions are converted into 41 00:02:56,940 --> 00:02:59,560 have a lot of good information in them. 42 00:02:59,560 --> 00:03:02,820 We'll learn exactly how this works in a later course. 43 00:03:02,820 --> 00:03:04,710 Now let's run our code again to see what we get. 44 00:03:08,310 --> 00:03:11,880 This is what we get when the exception is converted to a string. 45 00:03:11,880 --> 00:03:17,580 As you can see, the name of the exception thrown is System.NullReferenceException. 46 00:03:17,580 --> 00:03:22,620 This exception happens when we try to use null like a normal object. 47 00:03:22,620 --> 00:03:23,755 Look up here. 48 00:03:23,755 --> 00:03:27,650 GetLocationAt is supposed to return null if we ask for 49 00:03:27,650 --> 00:03:29,920 a step that isn't on the path. 50 00:03:29,920 --> 00:03:33,410 That means that the location variable here is now null. 51 00:03:35,390 --> 00:03:38,880 Now when we tried to get the X field from the location object, 52 00:03:38,880 --> 00:03:42,800 it can't do what we asked, because it isn't the location. 53 00:03:42,800 --> 00:03:46,320 This is null, and null is the absence of a location. 54 00:03:47,450 --> 00:03:51,130 This causes the null reference exception to be thrown. 55 00:03:51,130 --> 00:03:53,250 This is a very common exception to deal with. 56 00:03:54,280 --> 00:03:57,620 So, what should we do to make this code work? 57 00:03:57,620 --> 00:04:01,380 One thing we could do is check to see if location is null, 58 00:04:01,380 --> 00:04:04,740 before we try to print out x and y like this. 59 00:04:04,740 --> 00:04:10,586 So we can say, if(location 60 00:04:10,586 --> 00:04:15,960 != null then print this to the console. 61 00:04:15,960 --> 00:04:20,740 So if location is null, nothing will get printed and 62 00:04:20,740 --> 00:04:26,400 we won't be trying to access location.X or location.Y. 63 00:04:26,400 --> 00:04:29,890 This is a very common pattern when dealing with null. 64 00:04:29,890 --> 00:04:32,480 It's often called a null check. 65 00:04:32,480 --> 00:04:36,060 There are some interesting links in the teacher's notes if you want to dive deeper 66 00:04:36,060 --> 00:04:37,570 into how to handle null values. 67 00:04:39,110 --> 00:04:42,500 We'll learn other ways to deal with null in future courses, 68 00:04:42,500 --> 00:04:45,340 including how to avoid null entirely. 69 00:04:45,340 --> 00:04:49,760 A good practice when working with objects is to always consider what would happen 70 00:04:49,760 --> 00:04:52,490 if an object is null, and take the proper precautions. 71 00:04:53,520 --> 00:04:55,630 We've also just learned about arrays. 72 00:04:55,630 --> 00:04:57,890 Arrays are pretty handy, are they not? 73 00:04:57,890 --> 00:05:01,410 I mentioned before that arrays are just one of the many ways 74 00:05:01,410 --> 00:05:03,600 to store a collection of objects. 75 00:05:03,600 --> 00:05:05,960 We'll learn about those in other courses. 76 00:05:05,960 --> 00:05:07,100 So in the future, 77 00:05:07,100 --> 00:05:11,190 we might want to change our path class to use one of those instead of an array. 78 00:05:12,330 --> 00:05:17,040 We were smart and used encapsulation to hide the fact that we're using an array. 79 00:05:17,040 --> 00:05:20,130 We could change it from an array to any other type of collection. 80 00:05:20,130 --> 00:05:24,020 And it won't break any code that's using the public parts of the path class, 81 00:05:24,020 --> 00:05:25,550 because those parts won't change. 82 00:05:26,630 --> 00:05:28,680 Next we'll learn about properties. 83 00:05:28,680 --> 00:05:32,340 They're an important part of C# that helps with encapsulation. 84 00:05:32,340 --> 00:05:32,840 I can't wait.