Investigating a NullPointerException3:21 with Ben Jakuben
We have an error! It's a common one: we have encountered an uncaught NullPointerException. Let's see how to investigate and fix it.
So I just made an intentional but 0:00 very common mistake where I mistyped the name that we used in the other activity. 0:02 So if we run the app now it starts up and everything seems fine. 0:07 But if we type our name. 0:12 And start our adventure, we get an error. 0:16 Let's investigate and see what happened. 0:20 Here I want to bring up the Android monitor which again has our log cat view. 0:22 This is a lot of information and it's sometimes hard to see. 0:26 There's a button here that can change at the line breaks. 0:29 But I like to have it here and scroll over. 0:31 By the way you can see the error, it's nice and obvious. 0:33 It's highlighted in red whereas the rest of the stuff was black and blue. 0:36 I'm going to scroll over a little bit and 0:41 take a better look at the exception itself. 0:43 So let's unpack this a little bit. 0:46 We have a fatal exception in our main thread. 0:47 It's in our app interactive story and it is a java.lang.runtime exception. 0:50 Says it was unable to start the activity and it gives some details about it. 0:56 And this is called a stack trace because it traces through 0:59 the stack of method calls where the error handled and then was bubbled up. 1:02 So if we scroll down a little bit we can see that it was originally caused by 1:07 a NullPointerException where it says print line needs a message. 1:10 And if we dig back a little bit further, 1:15 we can see that it's from our log statement. 1:17 And sure enough we get to a line in StoryActivity 1:19 inside of our onCreate method, and it's clickable. 1:23 If we click on it, 1:25 we can see the offending line that threw the initial NullPointerException. 1:26 Our app crashed because we didn't handle that error. 1:31 The first thing we can do is avoid a hard crash by adding a null check 1:34 around this code. 1:38 This is good practice whenever we're getting data from an intent or 1:39 anywhere really. 1:42 We know in this simple app that we will always start StoryActivity from 1:43 MainActivity. 1:47 But in a more complex app we might be starting activities from multiple places. 1:47 And if we're not careful, we might not have the same exact intents from each one. 1:51 So let's reduce this Android monitor. 1:55 And we can still set the name variable here. 1:57 But after the fact, we want to add a new if statement. 2:01 if.name = null then we should give the name some kind of default value. 2:04 Let's set name = Friend instead. 2:11 So now, no matter what, our name variable will have a value. 2:16 Well, that's not totally true, what happens if the user doesn't type anything? 2:19 In that case, it would have a blank string. 2:23 It would just be blank. 2:25 That would affect how our story will look, so 2:25 we should also supply a default value if there is a blank string. 2:28 We can do that with another check here. 2:32 So if name is null or if name is empty, 2:35 name.isEmpty, we'll check if it's an empty string. 2:39 So first let's run this and see if it catches the exception for the wrong key. 2:44 And we can type the name, start the adventure and it didn't crash this time. 2:49 And if we go to log we should be able to find yep, there it is, Friend was used. 2:55 Now let's fix the typo with the key, change this to a lowercase n, and 3:00 run it again. 3:03 This time I'm not going to type anything, just click on the button. 3:06 And if we go back to the log, same thing, there we go. 3:09 A new string with the word Friend in it. 3:12 In the next video, we will address the key name by setting it in one master location, 3:15 the string resource file. 3:19
You need to sign up for Treehouse in order to download course files.Sign up