Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Local Development Environments!
You have completed Local Development Environments!
Preview
Learn how to explore and step through your code using the powerful debugger.
Learn more
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've talked about bugs or errors
in our code in the past,
0:00
but we never really talked
about where that term came from.
0:03
It's a bit of a lore these days,
but back in 1946, Grace Hopper
0:07
was having some problems with a program
she was running on the Mark II.
0:11
After trying to figure out the flaw
0:15
in her code for a while,
she found a moth trapped in the relay.
0:17
Once the bug was removed,
the program worked correctly.
0:20
The program was properly debugged.
0:23
In most of today's
popular programming languages,
0:27
there are a lot of tools provided
to assist with the removal of bugs.
0:30
They are called debuggers,
and they give you a better
0:33
overall understanding
of how the code is actually working.
0:37
Let's go learn
some bug extermination tricks.
0:41
Okay, so the first tool I'd like to use
0:44
to introduce you to your debugger
is something called breakpoints.
0:47
It's possible to tell the code to pause
0:51
at a certain line
by using a code breakpoint.
0:54
Adding breakpoints is simple.
0:57
Just click the line number
on the left side of the editor,
0:59
called the gutter,
where you want it to stop.
1:03
I was thinking we can do this
inside the karaoke machine's run method.
1:06
Let's stop on the line where it prompts
for an action, before actually prompting.
1:11
Just click the line number in the gutter.
1:17
It adds a red circle
which is a breakpoint.
1:20
Under the run menu, there's an option
to debug your current configuration.
1:23
I'm going to choose debug karaoke
and that's going to pop up the debugger.
1:28
Notice
how the line is now highlighted in blue?
1:34
That's
letting us know that we are paused here.
1:37
It's suspended.
1:40
You will also see a debugger
The debugger tab appears at the bottom.
1:41
Over here on the left side of the debugger
tab, it shows us how we got there.
1:45
This is a stack of code.
1:50
You've probably seen
these stack traces before.
1:52
Right now, we're in the run
method in Karaoke Machine.
1:55
But we got there from the main method.
1:59
If I click into main, you'll see
2:02
it flips to the correct file
and highlights how we got there.
2:04
It's a good way to trace your steps back
up, also called walking up the stack.
2:09
From here we can do a couple of things
2:14
We can move line by line
or dive deeper into code
2:16
Let do a deeper dive first Suppose
I want to look at the prompt action method
2:20
I can click back into the run
method in the debugger
2:26
Then I can click step into.
2:30
It's this arrow pointing down.
2:32
Or press F7.
2:34
This moves into the definition
2:36
of prompt action
inside the karaoke machine file.
2:39
Cool.
2:43
Now we're inside the prompt action method
and we're still suspended.
2:43
You'll also see
we now have the prompt action
2:48
method from run from main in the debugger.
2:51
So now we're three frames deep.
2:55
Also in the debugger tab
we have access to the variables.
2:59
For example mSongBook
which has a private variable mSongs,
3:02
and we can open those up and drill down into the songs. Pretty cool, right?
We're paused here, this first line in the promptAction method,
3:07
and if we Step Into again, it will move deeper.
IntelliJ has a built in "Smart Step" that automatically activates
3:16
to aid us when there are several
methods called on a line.
3:25
Smart Step is currently highlighting
the Get Song Count method.
3:29
If we press the down arrow
on our keyboard,
3:34
it will highlight the size method.
3:36
Let's press
the up arrow back into getSongCount
3:39
method and hit enter to step into it.
3:42
Now we're at the getSongCount
method in Songbook.
3:46
An important thing to note here,
if you step into a built-in Java method,
3:50
such as size, it will open up
where that is defined in the Java API.
3:55
the array.deck.java file
in the case of the size method.
4:01
We won't be diving into that,
but it's a great way to explore
4:06
APIs and demystify what's happening.
4:09
Let's press step into a few more times
4:12
until the for loop is highlighted in blue.
4:14
Then we'll step into the for loop.
4:17
Now it's about to run the line
where it prints out.
4:20
I'm going to step into it once more.
4:23
Remember, this loop is going through each menu option and setting the option variable.
As you step through, it will show the current value, for example, "add", then "play", and so on.
4:25
If you don want to watch the whole
for loop you can use Run to Cursor
4:39
Put your cursor outside the loop
and it will fast forward to that line
4:43
The Run to Cursor
option is in the More menu
4:48
which can be accessed
by clicking on these three vertical dots.
4:51
Cool right?
4:55
We're still inside prompt action, but
we skipped the tedious loop iterations.
4:56
You can also step out and go up
a frame, back where you came from.
5:01
Let's click the up arrow icon to step out.
5:06
Uh oh, now it looks like we're stuck.
5:09
There are no frames showing.
5:12
What's going on?
5:14
right.
5:16
Prompt action called readLine,
and it's waiting for the user's input.
5:17
Flip over to the console tab.
5:22
Enter,
choose, then go back to the debugger.
5:25
Now we're back in
5:29
run, paused at the choice line.
5:30
Let's step into the switch statement,
5:33
then step into the choose case.
5:36
Now we're on the prompt for singer name
method,
5:39
but I don't really want to see
the inner workings of it.
5:42
We can use
step over by clicking on this bent
5:46
arrow icon or pressing F8.
5:49
After stepping over,
we have no frame showing again.
5:52
You know what that means.
5:56
It's waiting for an input again.
5:57
Let's flip over to the console
and I'll type the singer's name as Laura.
5:59
Now back to the debugger. Great.
6:04
Let's press F8 again to step over
6:07
the prompt artist method call
and back to the console tab.
6:10
Now it's asking for the artist.
6:13
Let it enter without choosing an artist.
6:16
As expected, our program crashed.
6:18
Now we could go back through all the steps
of stepping down
6:21
and stepping over to get back to where we were.
6:25
Or, we can simply move the breakpoint to a new location.
6:29
Let's do that.
6:32
We'll set a new breakpoint at the spot
6:35
where we asked for the song,
so we can stop cleanly this time.
6:38
Alright, this time let's start
the debugger by clicking on this cute
6:42
bug icon on the top run bar.
6:46
I'll pick Choose and enter
my name as a singer again,
6:49
then 6 to choose Taylor Swift.
6:53
Perfect! It stopped on our new breakpoint.
6:55
Let step over the prompt song for artist
method call
6:59
Let flip over to the console
I choose one for ShakeItOff
7:03
So now we at the song request
Let's go ahead and step over that.
7:07
It's going to build a new song
request object.
7:13
Okay, awesome.
7:16
So, in our debugger,
we have this song request.
7:17
I wanna show you a cool little tool
that's a lot like the REPL
7:21
that we saw
that we can use to run our own code.
7:24
Let's click Run in the top menu bar,
7:28
then Debugging Actions,
then Evaluate Expression.
7:31
Or you can get there
by pressing Option F8.
7:36
Now we can run the code
while the program is paused.
7:40
I'll type songRequest.getSingerName
and click Evaluate.
7:44
Let's expand the height of
7:50
the Evaluate window
so that we can see the full result output.
7:51
Awesome.
7:55
You can even change variables
during Execution.
7:57
For example,
we can redefine the song request
7:59
to have a new singer, Gustavo.
8:02
Bam! Now Gustavo's a singer.
8:10
This expression evaluation
approach can be a handy way to test
8:13
different values when trying to simulate
and troubleshoot tricky edge cases.
8:16
When you're ready,
you can press the Resume Program button
8:21
and the program keeps running.
8:25
I'll pick
choose Gustavo and 7 for the cure.
8:28
It's now paused again at our breakpoint.
8:33
If you don't want a breakpoint anymore
you can toggle it off
8:36
by clicking the red circle.
8:39
Okay so that's a nice basic introduction
to some of the key
8:41
concepts of the powerful debugger tool.
8:45
This is one of those things
8:48
that most of the popular IDEs have
and they all kind of work the same way.
8:49
It's possible to have
the debugger kick in
8:55
when specific
exceptions are thrown.
8:56
As you can imagine, this is super handy.
8:59
It's also possible to launch it
when certain conditions are met,
9:02
even when fields are set or assessed.
9:05
I've added some notes
on where to learn more of this.
9:08
Check below in the teacher's notes.
9:11
It's a little out of the scope
of this course, but
9:13
I'd love to dive deeper into more advanced
features in an upcoming workshop.
9:16
Let's take a look at
how to work within the team using IntelliJ
9:21
right after this exercise.
9:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up