1 00:00:00,290 --> 00:00:03,740 We've talked about bugs or errors in our code in the past, but 2 00:00:03,740 --> 00:00:06,390 we've never really talked about where that term came from. 3 00:00:08,340 --> 00:00:11,300 It's a bit of lore these days, but back in 1946, 4 00:00:11,300 --> 00:00:14,890 Grace Hopper was having some problems with a program she was running on the Mark Two. 5 00:00:15,930 --> 00:00:17,960 After trying to figure out the flaw in her code for a while, 6 00:00:17,960 --> 00:00:21,020 she found a moth trapped in the relay. 7 00:00:21,020 --> 00:00:23,722 Once the bug was removed, the program worked correctly. 8 00:00:23,722 --> 00:00:27,711 [SOUND] The program was properly debugged. 9 00:00:27,711 --> 00:00:30,327 In most of today's popular programming languages, 10 00:00:30,327 --> 00:00:33,675 there are tools that are provided to assist the the removal of bugs. 11 00:00:33,675 --> 00:00:37,090 They are called debuggers, and they give you a better 12 00:00:37,090 --> 00:00:39,319 overall understanding of how the code is actually working. 13 00:00:40,320 --> 00:00:42,410 Let's go learn some bug extermination tricks. 14 00:00:43,970 --> 00:00:47,470 Okay, so the first tool I'd like to use to introduce you to your debugger 15 00:00:47,470 --> 00:00:49,610 is something called breakpoints. 16 00:00:49,610 --> 00:00:53,570 Now it's possible to tell the code to pause at a certain line, and 17 00:00:53,570 --> 00:00:56,720 you can do this with what is known as a code breakpoint. 18 00:00:56,720 --> 00:00:58,460 Adding breakpoints is simple, 19 00:00:58,460 --> 00:01:02,640 you just click the side of the editor over here where you want it to stop. 20 00:01:02,640 --> 00:01:06,610 This area is called the gutter, so I was thinking we could inside this 21 00:01:06,610 --> 00:01:11,380 KaraokeMachine run method, we'll stop on this line where it prompts for 22 00:01:11,380 --> 00:01:13,380 the action, before we prompt for the action. 23 00:01:13,380 --> 00:01:15,780 So I just clicked over here in this gutter, and 24 00:01:15,780 --> 00:01:20,960 I clicked and it added a little stop sign here which is a break point. 25 00:01:20,960 --> 00:01:25,970 And so under the run menu there is an option to debug your 26 00:01:25,970 --> 00:01:27,190 current configuration. 27 00:01:27,190 --> 00:01:32,370 So I'm going to do that, I'm going to choose debug karaoke and that's 28 00:01:32,370 --> 00:01:37,195 gonna pop up the debugger, and notice how the line is now highlighted in blue. 29 00:01:37,195 --> 00:01:42,310 That's the way of letting us know that we are paused here, it's suspended. 30 00:01:42,310 --> 00:01:47,600 So, you'll see there's a debugger tab down here on the bottom and 31 00:01:47,600 --> 00:01:51,060 over here under frames, what it's doing is it's showing us how we got there. 32 00:01:51,060 --> 00:01:52,550 So this is the stack of code. 33 00:01:52,550 --> 00:01:54,390 You've probably seen these in the stack traces, right? 34 00:01:54,390 --> 00:01:58,760 So, right now we're in run in karaoke machine, but we got there from here, 35 00:01:58,760 --> 00:01:59,480 from main. 36 00:01:59,480 --> 00:02:03,280 So if I click onto main, you'll see that it's flipped to the proper file and 37 00:02:03,280 --> 00:02:06,140 it's showing me highlighted there in blue, of how we got in there. 38 00:02:06,140 --> 00:02:08,790 So it's kinda a good way of oh, how did we get here? 39 00:02:08,790 --> 00:02:11,220 And kinda trace your steps back up, walk up the stack. 40 00:02:12,260 --> 00:02:14,610 So from here we can do a couple of things. 41 00:02:14,610 --> 00:02:17,780 We can move line by line or we can dive deeper into the code. 42 00:02:17,780 --> 00:02:19,140 Let's do a deeper dive first. 43 00:02:19,140 --> 00:02:22,370 So, let's say that I wanted to look at this promptAction method 44 00:02:22,370 --> 00:02:23,420 that's here, right? 45 00:02:23,420 --> 00:02:26,430 So I can come down here and I can choose the step into method. 46 00:02:26,430 --> 00:02:30,590 It's an arrow pointing, so step into, and that's F7, so 47 00:02:30,590 --> 00:02:35,000 what that's going to do is that's going to go into the definition of prompt action 48 00:02:35,000 --> 00:02:37,110 on the KaraokeMachine file, right? 49 00:02:37,110 --> 00:02:39,970 So, we've now stepped in to this code. 50 00:02:39,970 --> 00:02:43,380 Cool, so now you'll see that we're inside the prompt action method definition and 51 00:02:43,380 --> 00:02:45,050 we're still suspended. 52 00:02:45,050 --> 00:02:47,861 And you'll see over here in the frames, 53 00:02:47,861 --> 00:02:52,337 you'll see that we're now in prompt action from run from main. 54 00:02:52,337 --> 00:02:55,390 So there's our stack, we're all the way down into prompt action. 55 00:02:55,390 --> 00:03:00,110 And you can also see in the view over here that we have access to these variables. 56 00:03:00,110 --> 00:03:05,030 So, if we look at this here's the mSongBook that's private to the karaoke machine 57 00:03:05,030 --> 00:03:08,570 and it has a private variable that mSongbook does called mSongs. 58 00:03:08,570 --> 00:03:11,060 And we can open those up and we can take a look at the different songs and 59 00:03:11,060 --> 00:03:13,100 we can keep on drilling down. 60 00:03:13,100 --> 00:03:14,420 It's pretty cool, right? 61 00:03:14,420 --> 00:03:18,780 So, we're paused on this line here, and if we step into this one, 62 00:03:20,780 --> 00:03:23,440 so it's a single line that's on multiple lines, but 63 00:03:23,440 --> 00:03:25,930 it's one line of code that's just spaced out better. 64 00:03:25,930 --> 00:03:30,040 So if we step into this we're going to go and do the getSongCount on the SongBook. 65 00:03:30,040 --> 00:03:31,630 So let's do that. 66 00:03:31,630 --> 00:03:36,003 So now over here we're in the getSongCount from promptAction from run from main. 67 00:03:36,003 --> 00:03:37,105 So in getSongCount, 68 00:03:37,105 --> 00:03:41,207 if we press the step into you might think that it's going to do the size but we're 69 00:03:41,207 --> 00:03:46,090 going to be blocked cuz we're really only concerned with what our code is, right? 70 00:03:46,090 --> 00:03:49,730 So it jumped back out because it returned that value. 71 00:03:49,730 --> 00:03:53,310 If you wanted to step in the code, this red one here, 72 00:03:53,310 --> 00:03:57,540 this Force Step Into, this will actually go into the Java code. 73 00:03:57,540 --> 00:03:58,640 We're not gonna do that right now, but 74 00:03:58,640 --> 00:04:00,590 I just want to let you know that that's available. 75 00:04:00,590 --> 00:04:03,850 If you wanted to go do some more exploring of what the Java API's are actually doing, 76 00:04:03,850 --> 00:04:06,910 cuz most of them are going to show you Java code and 77 00:04:06,910 --> 00:04:09,040 you can kind of walk through and see what things are doing. 78 00:04:09,040 --> 00:04:11,720 And this is a great way to explore the APIs and 79 00:04:11,720 --> 00:04:14,250 demystify what's actually happening. 80 00:04:14,250 --> 00:04:19,300 Okay, so let's just go ahead and step into this for loop, here. 81 00:04:19,300 --> 00:04:22,300 Oh, so now it's gonna run the line where it does the print. 82 00:04:22,300 --> 00:04:25,460 Okay, so I'm gonna go ahead and I'm gonna quick step into, one more time, and 83 00:04:25,460 --> 00:04:27,670 we're gonna step into the for loop. 84 00:04:27,670 --> 00:04:30,630 And so now remember what this loop is doing is it's looping through 85 00:04:30,630 --> 00:04:35,310 each of the menu options we have and its setting the variable option for each loop. 86 00:04:35,310 --> 00:04:38,790 So, now we can look at this first one is add, and so 87 00:04:38,790 --> 00:04:43,720 let's just go ahead and we'll step into it step into it. 88 00:04:43,720 --> 00:04:47,720 And you'll see as you move it puts the value of what's going on over there. 89 00:04:47,720 --> 00:04:48,378 It's pretty cool. 90 00:04:50,461 --> 00:04:52,086 So, we'll step into that. 91 00:04:52,086 --> 00:04:54,295 Okay cool, so now we're on the next one. 92 00:04:56,878 --> 00:05:02,470 Here which should give us play, which is the next option on there. 93 00:05:02,470 --> 00:05:04,920 So, okay, so we kind of understand what this is doing, right? 94 00:05:04,920 --> 00:05:07,880 So, we don't need to watch the whole for loop go through, so 95 00:05:07,880 --> 00:05:13,200 what we could do is we could put our cursor outside and this function here. 96 00:05:13,200 --> 00:05:17,220 Run to cursor will make the code all run until we get to there. 97 00:05:18,430 --> 00:05:19,040 Cool, right? 98 00:05:19,040 --> 00:05:22,496 So now we're still paused inside of the prompt action, but 99 00:05:22,496 --> 00:05:25,590 we jump through that for loop and all that code ran. 100 00:05:25,590 --> 00:05:27,390 But we didn't need to step and watch each one of them. 101 00:05:28,400 --> 00:05:32,300 So, we can also step out of methods and go back to the frame where we started, okay? 102 00:05:32,300 --> 00:05:36,530 So, that's this arrow, of course, the one stepping up and that's called Step Out and 103 00:05:36,530 --> 00:05:37,780 that's Shift F8 to get out. 104 00:05:37,780 --> 00:05:39,940 So, we're just gonna go up the stack. 105 00:05:41,400 --> 00:05:45,120 Okay, and now it looks like it paused so there's no frames that are available. 106 00:05:45,120 --> 00:05:47,440 So, What's going on? 107 00:05:47,440 --> 00:05:48,070 Oh, that's right. 108 00:05:48,070 --> 00:05:51,360 Prompt action, right here, this line called read line. 109 00:05:51,360 --> 00:05:53,850 It's waiting for us to answer the action. 110 00:05:53,850 --> 00:05:56,280 So, let's go ahead, and let's say choose. 111 00:05:56,280 --> 00:05:59,940 That's the thing that we wanna do from the menu, we wanna choose to sing a song. 112 00:06:01,260 --> 00:06:01,830 Cool, okay. 113 00:06:01,830 --> 00:06:03,050 Now bam. Now we're back, and 114 00:06:03,050 --> 00:06:04,580 the debugger's stopped. 115 00:06:04,580 --> 00:06:07,150 We're back in run, we're at this choice line here. 116 00:06:07,150 --> 00:06:12,550 Cool, so let's just go ahead and let's step here and again, 117 00:06:12,550 --> 00:06:16,990 so now we typed in the choice to be choose, so we're on the switch statement, 118 00:06:16,990 --> 00:06:19,270 so let's go ahead and let's step into this choose statement. 119 00:06:20,510 --> 00:06:22,920 Okay, so we just wrote this prompt for singer name. 120 00:06:22,920 --> 00:06:25,881 I don't really want to see how it works, so 121 00:06:25,881 --> 00:06:30,754 the next feature is here, there's this step over, and that's F8. 122 00:06:30,754 --> 00:06:34,000 Okay, so if I press F8, it's gonna step over that, and 123 00:06:34,000 --> 00:06:36,560 it looks like again like it paused. 124 00:06:36,560 --> 00:06:40,420 Oh, that's right it's prompting, so I'm gonna flip over to the console, I'm gonna 125 00:06:40,420 --> 00:06:45,310 enter the singer's name which is me, and now I'm gonna go back to the debugger. 126 00:06:45,310 --> 00:06:46,350 Oh there, okay we're stopped. 127 00:06:46,350 --> 00:06:51,740 I'm gonna press F8 again, stopped again, it's prompting for the artist. 128 00:06:52,750 --> 00:06:55,825 All right, let's say Wilson Phillips, what do you say? 129 00:06:55,825 --> 00:07:02,907 Okay, so I think I pressed enter without pressing nine. 130 00:07:02,907 --> 00:07:05,480 All right, so let's come back. 131 00:07:05,480 --> 00:07:09,961 Let's undo this break point, and let's do this break point. 132 00:07:09,961 --> 00:07:14,545 Okay, so we're gonna get right to here. 133 00:07:14,545 --> 00:07:17,140 Actually, let's get to the point where we ask for the song, okay? 134 00:07:17,140 --> 00:07:19,250 So we can continue through and let it stop, okay? 135 00:07:19,250 --> 00:07:20,140 So I'm gonna start that again. 136 00:07:20,140 --> 00:07:22,581 So up here, on the debugger, there's karaoke play, 137 00:07:22,581 --> 00:07:25,295 there's also a little debug icon here, so I can click this. 138 00:07:28,671 --> 00:07:30,171 Okay, so what do you wanna do? 139 00:07:30,171 --> 00:07:32,336 We wanna choose a song. 140 00:07:32,336 --> 00:07:37,006 It's me, I wanna to listen to some Wilson Phillips, boom and it's stopped. 141 00:07:37,006 --> 00:07:38,827 Awesome, right? 142 00:07:38,827 --> 00:07:43,400 Okay, so let's step over the prompts song for artists and 143 00:07:43,400 --> 00:07:46,920 again flip over to the console, let's hold on for one more day. 144 00:07:48,990 --> 00:07:52,430 So, now we're at the song request. 145 00:07:52,430 --> 00:07:54,060 Let's go ahead and step over that. 146 00:07:54,060 --> 00:07:56,462 It's going to build a the new SongRequest object. 147 00:07:58,628 --> 00:07:59,520 Okay, awesome. 148 00:07:59,520 --> 00:08:03,837 So, in our debugger we have this song request. 149 00:08:03,837 --> 00:08:08,134 And I want to show you a little tool that's a lot like the REPL that we saw 150 00:08:08,134 --> 00:08:10,360 that we can use to run our own code. 151 00:08:10,360 --> 00:08:17,045 So if you choose run and then you choose evaluate expression. 152 00:08:17,045 --> 00:08:18,240 Okay, so option F8. 153 00:08:19,790 --> 00:08:21,440 You can actually run some codes. 154 00:08:21,440 --> 00:08:24,860 So, remember what we were looking at here is we're looking at song requests. 155 00:08:24,860 --> 00:08:30,332 Right, so if we say song, oh look at that, it auto completes here too. 156 00:08:30,332 --> 00:08:33,045 Song request, type in singer name. 157 00:08:35,628 --> 00:08:38,280 It will run and this is the result that came back. 158 00:08:38,280 --> 00:08:41,711 So, that's very similar to the REPL that we were using, right? 159 00:08:41,711 --> 00:08:45,200 So we can actually even change the state here in the program. 160 00:08:45,200 --> 00:08:47,510 So, that's also a nice fun feature. 161 00:08:47,510 --> 00:08:54,890 So I'm going to go ahead and I'm gonna redefine what songRequest is, okay? 162 00:08:54,890 --> 00:09:00,160 So remember we're stopped on this line here so 163 00:09:00,160 --> 00:09:04,540 I'm gonna say songRequest = new SongRequest. 164 00:09:04,540 --> 00:09:07,310 Let's actually sign Ben up to sing that, right? 165 00:09:08,330 --> 00:09:09,530 He'd probably sing that one really good. 166 00:09:11,080 --> 00:09:14,910 Okay, so I'm going to press enter, and bam, so it got set. 167 00:09:14,910 --> 00:09:18,953 Okay, so now I come back here and I look at song request, 168 00:09:18,953 --> 00:09:24,045 Ben is the singer, even though the singer name that I said before was. 169 00:09:24,045 --> 00:09:27,507 So, it's kinda cool, so you kind of punch variables into different situations if 170 00:09:27,507 --> 00:09:30,410 you're trying to catch a certain thing in there. 171 00:09:30,410 --> 00:09:34,390 Okay, so we can press the button over here to resume program execution. 172 00:09:35,820 --> 00:09:38,770 Okay, and that's gonna keep on going and we're stuck here at the console again. 173 00:09:38,770 --> 00:09:43,390 So if we did choose, we'll sing another one. 174 00:09:43,390 --> 00:09:44,690 This time let's sing The Cure. 175 00:09:45,840 --> 00:09:47,480 Okay, bam. So it stopped again. 176 00:09:47,480 --> 00:09:49,150 So it'll keep on going. 177 00:09:49,150 --> 00:09:50,930 So you can also toggle these on and 178 00:09:50,930 --> 00:09:55,866 off using the Cmd+F8 key, will toggle the breakpoint on and off. 179 00:09:55,866 --> 00:09:59,540 Or Ctrl+F8 on Windows. 180 00:09:59,540 --> 00:10:03,870 So now when I press resume, it will keep going and not stop. 181 00:10:06,940 --> 00:10:08,910 Cool, welcome to your debugger. 182 00:10:08,910 --> 00:10:12,430 Okay, so that's a nice basic introduction to some of the key concepts 183 00:10:12,430 --> 00:10:14,580 of the powerful debugger tool. 184 00:10:14,580 --> 00:10:17,620 This is one of those things that most of the popular IDE's have and 185 00:10:17,620 --> 00:10:20,140 they all kind of work the same way. 186 00:10:20,140 --> 00:10:24,440 It is possible to have the debugger kick in when specific exceptions are thrown. 187 00:10:24,440 --> 00:10:27,030 As you can imagine, this is super handy. 188 00:10:27,030 --> 00:10:29,870 It's also possible to launch it when certain conditions are met, 189 00:10:29,870 --> 00:10:32,080 even when fields are set or accessed. 190 00:10:32,080 --> 00:10:34,590 I've added some notes on where to learn more of this. 191 00:10:34,590 --> 00:10:36,460 Check below in the teacher's notes. 192 00:10:36,460 --> 00:10:38,120 It's a little out of the scope of this course, but 193 00:10:38,120 --> 00:10:41,500 I'd love to dive deeper into more advanced features in an upcoming workshop. 194 00:10:42,820 --> 00:10:46,090 Let's take a look at how to work with a team using IntelliJ, 195 00:10:46,090 --> 00:10:46,910 right after this exercise.