Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Learn how to explore and step through your code using the powerful debugger.
Learn more
-
0:00
We've talked about bugs or errors in our code in the past, but
-
0:03
we've never really talked about where that term came from.
-
0:08
It's a bit of lore these days, but back in 1946,
-
0:11
Grace Hopper was having some problems with a program she was running on the Mark Two.
-
0:15
After trying to figure out the flaw in her code for a while,
-
0:17
she found a moth trapped in the relay.
-
0:21
Once the bug was removed, the program worked correctly.
-
0:23
[SOUND] The program was properly debugged.
-
0:27
In most of today's popular programming languages,
-
0:30
there are tools that are provided to assist the the removal of bugs.
-
0:33
They are called debuggers, and they give you a better
-
0:37
overall understanding of how the code is actually working.
-
0:40
Let's go learn some bug extermination tricks.
-
0:43
Okay, so the first tool I'd like to use to introduce you to your debugger
-
0:47
is something called breakpoints.
-
0:49
Now it's possible to tell the code to pause at a certain line, and
-
0:53
you can do this with what is known as a code breakpoint.
-
0:56
Adding breakpoints is simple,
-
0:58
you just click the side of the editor over here where you want it to stop.
-
1:02
This area is called the gutter, so I was thinking we could insight this
-
1:06
karaoke machine run method, we'll stop on this line where it prompts for
-
1:11
the action, before we prompt for the action.
-
1:13
So I just clicked over here in this gutter, and
-
1:15
I clicked and it added a little stop sign here which is a break point.
-
1:20
And so under the run menu there is an option to debug your
-
1:25
current configuration.
-
1:27
So I'm going to do that, I'm going to choose debug karaoke and that's
-
1:32
gonna pop up the debugger, and notice how the line is now highlighted in blue.
-
1:37
That's the way of letting us know that we are paused here, it's suspended.
-
1:42
So, you'll see there's a debugger tab down here on the bottom and
-
1:47
over here under frames, what it's doing is it's showing us how we got there.
-
1:51
So this is the stack of code.
-
1:52
You've probably seen these in the stack traces, right?
-
1:54
So, right now we're in run in karaoke machine, but we got there from here,
-
1:58
from main.
-
1:59
So if I click onto main, you'll see that it's flipped to the proper file and
-
2:03
it's showing me highlighted there in blue, of how we got in there.
-
2:06
So it's kinda a good way of oh, how did we get here?
-
2:08
And kinda trace your steps back up, walk up the stack.
-
2:12
So from here we can do a couple of things.
-
2:14
We can move line by line or we can dive deeper into the code.
-
2:17
Let's do a deeper dive first.
-
2:19
So, let's say that I wanted to look at this promptAction method
-
2:22
that's here, right?
-
2:23
So I can come down here and I can choose the step into method.
-
2:26
It's an arrow pointing, so step into, and that's F7, so
-
2:30
what that's going to do is that's going to go into the definition of prompt action
-
2:35
on the karaoke machine file, right?
-
2:37
So, we've now stepped in to this code.
-
2:39
Cool, so now you'll see that we're inside the prompt action method definition and
-
2:43
we're still suspended.
-
2:45
And you'll see over here in the frames,
-
2:47
you'll see that we're now in prompt action from run from main.
-
2:52
So there's our stack, we're all the way down into prompt action.
-
2:55
And you can also see in the view over here that we have access to these variables.
-
3:00
So, if we look at this here's the mSong book that's private to the karaoke machine
-
3:05
and it has a private variable that mSongbook does called mSongs.
-
3:08
And we can open those up and we can take a look at the different songs and
-
3:11
we can keep on drilling down.
-
3:13
It's pretty cool, right?
-
3:14
So, we're paused on this line here, and if we step into this one,
-
3:20
so it's a single line that's on multiple lines, but
-
3:23
it's one line of code that's just spaced out better.
-
3:25
So if we step into this we're going to go and do the getSongCount on the SongBook.
-
3:30
So let's do that.
-
3:31
So now over here we're in the getSongCount from promptAction from run from main.
-
3:36
So in getSongCount,
-
3:37
if we press the step into you might think that it's going to do the size but we're
-
3:41
going to be blocked cuz we're really only concerned with what our code is, right?
-
3:46
So it jumped back out because it returned that value.
-
3:49
If you wanted to step in the code, this red one here,
-
3:53
this Force Step Into, this will actually go into the Java code.
-
3:57
We're not gonna do that right now, but
-
3:58
I just want to let you know that that's available.
-
4:00
If you wanted to go do some more exploring of what the Java API's are actually doing,
-
4:03
cuz most of them are going to show you Java code and
-
4:06
you can kind of walk through and see what things are doing.
-
4:09
And this is a great way to explore the APIs and
-
4:11
demystify what's actually happening.
-
4:14
Okay, so let's just go ahead and step into this for loop, here.
-
4:19
Oh, so now it's gonna run the line where it does the print.
-
4:22
Okay, so I'm gonna go ahead and I'm gonna quick step into, one more time, and
-
4:25
we're gonna step into the for loop.
-
4:27
And so now remember what this loop is doing is its looping through
-
4:30
each of the menu options we have and its setting the variable option for each loop.
-
4:35
So, now we can look at this first one is add, and so
-
4:38
lets just go ahead and we'll step into it step into it.
-
4:43
And you'll see as you move it puts the value of whats going on over there.
-
4:47
It's pretty cool.
-
4:50
So, we'll step into that.
-
4:52
Okay cool, so now we're on the next one.
-
4:56
Here which should give us play, which is the next option on there.
-
5:02
So, okay, so we kind of understand what this is doing, right?
-
5:04
So, we don't need to watch the whole for loop go through, so
-
5:07
what we could do is we could put our cursor outside and this function here.
-
5:13
Run to cursor will make the code all run until we get to there.
-
5:18
Cool, right?
-
5:19
So now we're still paused inside of the prompt action, but
-
5:22
we jump through that for loop and all that code ran.
-
5:25
But we didn't need to step and watch each one of them.
-
5:28
So, we can also step out of methods and go back to the frame where we started, okay?
-
5:32
So, that's this arrow, of course, the one stepping up and that's called Step Out and
-
5:36
that's Shift F8 to get out.
-
5:37
So, we're just gonna go up the stack.
-
5:41
Okay, and now it looks like it paused so there's no frames that are available.
-
5:45
So, What's going on?
-
5:47
Oh, that's right.
-
5:48
Prompt action, right here, this line called read line.
-
5:51
It's waiting for us to answer the action.
-
5:53
So, let's go ahead, and let's say choose.
-
5:56
That's the thing that we wanna do from the menu, we wanna choose to sing a song.
-
6:01
Cool, okay.
-
6:01
Now bam. Now we're back, and
-
6:03
the debugger's stopped.
-
6:04
We're back in run, we're at this choice line here.
-
6:07
Cool, so let's just go ahead and let's step here and again,
-
6:12
so now we typed in the choice to be choose, so we're on the switch statement,
-
6:16
so let's go ahead and let's step into this choose statement.
-
6:20
Okay, so we just wrote this prompt for singer name.
-
6:22
I don't really want to see how it works, so
-
6:25
the next feature is here, there's this step over, and that's F8.
-
6:30
Okay, so if I press F8, it's gonna step over that, and
-
6:34
it looks like again like it paused.
-
6:36
Oh, that's right it's prompting, so I'm gonna flip over to the console, I'm gonna
-
6:40
enter the singer's name which is me, and now I'm gonna go back to the debugger.
-
6:45
Oh there, okay we're stopped.
-
6:46
I'm gonna press F8 again, stopped again, it's prompting for the artist.
-
6:52
All right, let's say Wilson Phillips, what do you say?
-
6:55
Okay, so I think I pressed enter without pressing nine.
-
7:02
All right, so let's come back.
-
7:05
Let's undo this break point, and let's do this break point.
-
7:09
Okay, so we're gonna get right to here.
-
7:14
Actually, let's get to the point where we ask for the song, okay?
-
7:17
So we can continue through and let it stop, okay?
-
7:19
So I'm gonna start that again.
-
7:20
So up here, on the debugger, there's karaoke play,
-
7:22
there's also a little debug icon here, so I can click this.
-
7:28
Okay, so what do you wanna do?
-
7:30
We wanna choose a song.
-
7:32
It's me, I wanna to listen to some Wilson Phillips, boom and it's stopped.
-
7:37
Awesome, right?
-
7:38
Okay, so let's step over the prompts song for artists and
-
7:43
again flip over to the counsel, let's hold on for one more day.
-
7:48
So, now we're at the song request.
-
7:52
Let's go ahead and step over that.
-
7:54
It's going to build a the new song request object.
-
7:58
Okay, awesome.
-
7:59
So, in our debugger we have this song request.
-
8:03
And I want to show you a little tool that's a lot like the ripple that we saw
-
8:08
that we can use to run our own code.
-
8:10
So if you choose run and then you choose evaluate expression.
-
8:17
Okay, so option F8.
-
8:19
You can actually run some codes.
-
8:21
So, remember what we were looking at here is we're looking at song requests.
-
8:24
Right, so if we say song, oh look at that, it auto completes here too.
-
8:30
Song request, type in singer name.
-
8:35
It will run and this is the result that came back.
-
8:38
So, that's very similar to the ripple that we were using, right?
-
8:41
So we can actually even change the state here in the program.
-
8:45
So, that's also a nice fun feature.
-
8:47
So I'm going to go ahead and I'm gonna redefine what song request is, okay?
-
8:54
So remember we're stopped on this line here so
-
9:00
I'm gonna say songRequest = new SongRequest.
-
9:04
Once actually sign Ben up to sing that, right?
-
9:08
He'd probably sing that one really good.
-
9:11
Okay, so I'm going to press enter, and bam, so it got set.
-
9:14
Okay, so now I come back here and I look at song request,
-
9:18
Ben is the singer, even though the singer name that I said before was.
-
9:24
So, it's kinda cool, so you kind of punch variables into different situations if
-
9:27
you're trying to catch a certain thing in there.
-
9:30
Okay, so we can press the button over here to resume program execution.
-
9:35
Okay, and that's gonna keep on going and we're stuck here at the console again.
-
9:38
So if we did choose, we'll sing another one.
-
9:43
This time let's sing The Cure.
-
9:45
Okay, bam. So it stopped again.
-
9:47
So it'll keep on going.
-
9:49
So you can also toggle these on and
-
9:50
off using the Cmd+F8 key, will toggle the breakpoint on and off.
-
9:55
Or Ctrl+F8 on Windows.
-
9:59
So now when I press resume, it will keep going and not stop.
-
10:06
Cool, welcome to your debugger.
-
10:08
Okay, so that's a nice basic introduction to some of the key concepts
-
10:12
of the powerful debugger tool.
-
10:14
This is one of those things that most of the popular IDE's have and
-
10:17
they all kind of work the same way.
-
10:20
It is possible to have the debugger kick in when specific exceptions are thrown.
-
10:24
As you can imagine, this is super handy.
-
10:27
It's also possible to launch it when certain conditions are met,
-
10:29
even when fields are set or accessed.
-
10:32
I've added some notes on where to learn more of this.
-
10:34
Check below in the teacher's notes.
-
10:36
It's a little out of the scope of this course, but
-
10:38
I'd love to dive deeper into more advanced features in an upcoming workshop.
-
10:42
Let's take a look at how to work with a team using Intelliga,
-
10:46
right after this exercise
You need to sign up for Treehouse in order to download course files.
Sign up