1 00:00:00,690 --> 00:00:02,590 So I just made an intentional but 2 00:00:02,590 --> 00:00:07,560 very common mistake where I mistyped the name that we used in the other activity. 3 00:00:07,560 --> 00:00:12,480 So if we run the app now it starts up and everything seems fine. 4 00:00:12,480 --> 00:00:13,905 But if we type our name. 5 00:00:16,959 --> 00:00:20,233 And start our adventure, we get an error. 6 00:00:20,233 --> 00:00:22,530 Let's investigate and see what happened. 7 00:00:22,530 --> 00:00:26,550 Here I want to bring up the Android monitor which again has our log cat view. 8 00:00:26,550 --> 00:00:29,660 This is a lot of information and it's sometimes hard to see. 9 00:00:29,660 --> 00:00:31,890 There's a button here that can change at the line breaks. 10 00:00:31,890 --> 00:00:33,870 But I like to have it here and scroll over. 11 00:00:33,870 --> 00:00:36,600 By the way you can see the error, it's nice and obvious. 12 00:00:36,600 --> 00:00:39,780 It's highlighted in red whereas the rest of the stuff was black and blue. 13 00:00:41,290 --> 00:00:43,100 I'm going to scroll over a little bit and 14 00:00:43,100 --> 00:00:44,980 take a better look at the exception itself. 15 00:00:46,010 --> 00:00:47,520 So let's unpack this a little bit. 16 00:00:47,520 --> 00:00:50,880 We have a fatal exception in our main thread. 17 00:00:50,880 --> 00:00:56,080 It's in our app interactive story and it is a java.lang.runtime exception. 18 00:00:56,080 --> 00:00:59,620 Says it was unable to start the activity and it gives some details about it. 19 00:00:59,620 --> 00:01:02,950 And this is called a stack trace because it traces through 20 00:01:02,950 --> 00:01:07,430 the stack of method calls where the error handled and then was bubbled up. 21 00:01:07,430 --> 00:01:10,740 So if we scroll down a little bit we can see that it was originally caused by 22 00:01:10,740 --> 00:01:15,680 a NullPointerException where it says print line needs a message. 23 00:01:15,680 --> 00:01:17,580 And if we dig back a little bit further, 24 00:01:17,580 --> 00:01:19,540 we can see that it's from our log statement. 25 00:01:19,540 --> 00:01:23,220 And sure enough we get to a line in StoryActivity 26 00:01:23,220 --> 00:01:25,460 inside of our onCreate method, and it's clickable. 27 00:01:25,460 --> 00:01:26,390 If we click on it, 28 00:01:26,390 --> 00:01:31,290 we can see the offending line that threw the initial NullPointerException. 29 00:01:31,290 --> 00:01:34,210 Our app crashed because we didn't handle that error. 30 00:01:34,210 --> 00:01:38,210 The first thing we can do is avoid a hard crash by adding a null check 31 00:01:38,210 --> 00:01:39,820 around this code. 32 00:01:39,820 --> 00:01:42,540 This is good practice whenever we're getting data from an intent or 33 00:01:42,540 --> 00:01:43,710 anywhere really. 34 00:01:43,710 --> 00:01:47,001 We know in this simple app that we will always start StoryActivity from 35 00:01:47,001 --> 00:01:47,870 MainActivity. 36 00:01:47,870 --> 00:01:51,720 But in a more complex app we might be starting activities from multiple places. 37 00:01:51,720 --> 00:01:55,940 And if we're not careful, we might not have the same exact intents from each one. 38 00:01:55,940 --> 00:01:57,770 So let's reduce this Android monitor. 39 00:01:57,770 --> 00:02:01,240 And we can still set the name variable here. 40 00:02:01,240 --> 00:02:04,518 But after the fact, we want to add a new if statement. 41 00:02:04,518 --> 00:02:11,400 if name = null then we should give the name some kind of default value. 42 00:02:11,400 --> 00:02:15,521 Let's set name = Friend instead. 43 00:02:16,740 --> 00:02:19,990 So now, no matter what, our name variable will have a value. 44 00:02:19,990 --> 00:02:23,280 Well, that's not totally true, what happens if the user doesn't type anything? 45 00:02:23,280 --> 00:02:25,110 In that case, it would have a blank string. 46 00:02:25,110 --> 00:02:25,910 It would just be blank. 47 00:02:25,910 --> 00:02:28,500 That would affect how our story will look, so 48 00:02:28,500 --> 00:02:32,850 we should also supply a default value if there is a blank string. 49 00:02:32,850 --> 00:02:35,550 We can do that with another check here. 50 00:02:35,550 --> 00:02:39,177 So if name is null or if name is empty, 51 00:02:39,177 --> 00:02:42,520 name.isEmpty, we'll check if it's an empty string. 52 00:02:44,000 --> 00:02:49,824 So first let's run this and see if it catches the exception for the wrong key. 53 00:02:49,824 --> 00:02:55,185 And we can type the name, start the adventure and it didn't crash this time. 54 00:02:55,185 --> 00:03:00,130 And if we go to log we should be able to find yep, there it is, Friend was used. 55 00:03:00,130 --> 00:03:03,900 Now let's fix the typo with the key, change this to a lowercase n, and 56 00:03:03,900 --> 00:03:04,420 run it again. 57 00:03:06,290 --> 00:03:09,570 This time I'm not going to type anything, just click on the button. 58 00:03:09,570 --> 00:03:12,792 And if we go back to the log, same thing, there we go. 59 00:03:12,792 --> 00:03:15,450 A new string with the word Friend in it. 60 00:03:15,450 --> 00:03:19,660 In the next video, we will address the key name by setting it in one master location, 61 00:03:19,660 --> 00:03:20,750 the string resource file.