This workshop will be retired on May 1, 2025.
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 Debugging an Existing Java Application!
You have completed Debugging an Existing Java Application!
Preview
Let's discuss stack frames and explore how they are exposed in the debugger.
Learn more
- Frames explained in the JVM documentation.
- Stack JavaDoc data structure (This is the data structure used to store the frames)
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
To use a debugger properly
within an application
0:00
it's fairly important to
have a grasp of terminology.
0:03
Now you probably have a pretty
good understanding of how this is
0:05
working already.
0:08
But might not have all the terms in place.
0:09
So let's explore this a little bit using
these index cards that I have here.
0:11
So all programs start
off with a call stack.
0:15
So let's imagine a pretty
straightforward application, right?
0:18
It has a main method.
0:20
This card here is representing a frame,
and the collection of cards here,
0:22
even though there's only one at
the moment, is called the stack.
0:25
So, let's assume that this card here,
the starting of our program,
0:29
it's the main method.
0:32
Inside that main method, let's imagine
that there's a call to another method that
0:33
runs some code that's used to get
the user's first name, right?
0:37
And the return value from that
method is the user's first name.
0:41
So, when that prompt method is called,
0:44
here in the main method,
a new frame is added to the stack.
0:47
This addition is called a push, right?
0:51
We push on to the frame.
0:53
And now, you can see that this
stack kinda makes sense, right?
0:54
It's a stack of frames, the frames
are stacked on top of each other.
0:57
So, this frame executes.
1:01
We prompt the user and
we get back the value.
1:03
And when we return our value,
1:05
the frame is removed from
the stack since it's complete.
1:07
This is called popping.
1:10
The frame is popped off the stack,
and we're back at the original frame.
1:11
And the result is available
in this frame scope.
1:15
So, as you can imagine,
each of these frames can contain code that
1:18
calls a new frame to be
pushed onto the stack.
1:22
And of course those frames can also
push frames and so on and so on and
1:25
the stack can get pretty tall, right.
1:29
So our spring boot flash card
application that we've been working with
1:32
also has a main method.
1:36
And in that main method,
it starts off a spring application.
1:37
And that spring application
kicks off several threads.
1:40
And each of those threads
has its own call stack.
1:44
And this one here maybe it's setting up
a web server, right, so it does some work.
1:48
This one here might be
listening to requests.
1:52
So it is listening,
has some things waiting.
1:53
And then this one here.
1:57
It's also kicking off its own stack.
1:59
And the code to process this
request is called, right, and
2:01
eventually it gets to our code
which is in a frame of its own.
2:04
And it pushes frames, and
we call methods like our services and
2:07
that calls another one or DAO which
calls the query methods for our data and
2:11
then the return rate, right?
2:15
So we pop when we get the data back and
we pop when we get the data back from
2:17
the service, and we pop when we
get the data back from the DAO.
2:20
Now let's take a look at how these
concepts are manifested in the debugger.
2:22
Okay, so
2:27
now that we've merged things on GitHub,
we need to update our local repository.
2:27
Now, I'm gonna flip back to the master
branch, and choose Checkout.
2:31
And now, what I'm gonna do
is I'm gonna choose VCS and
2:37
I'm gonna update the project.
2:41
And this default is fine,
the Branch default is using stash and
2:43
I'm gonna click OK.
2:47
Well, I just did that a second ago but
you probably see a little bit different.
2:48
You'll see some different
files there that changed.
2:52
So before we grab our next ticket,
let's go ahead and
2:55
take a look at how the app is working.
2:58
We'll kind of navigate around a bit,
so it's gonna come over here and
3:00
let's see this get started here,
what happens when we click this.
3:04
Well if we look at the bottom we can
see that it's called flashcard/next.
3:07
See that link in the bottom there.
3:12
So if we go over to our editor and
we do a find.
3:13
And we look for and
that was Cmd+Shift+F there.
3:17
If we look for
flashcard/next which is the URI there.
3:21
We'll find it.
3:27
So here's a request mapping for
a flashcard/next.
3:28
Let's go take a look.
3:30
Awesome.
This is the controller we're the
3:32
FlashCardController file, and
let's drop a breakpoint right here.
3:34
And, I'm gonna flip over to the debugger,
and I'm gonna rerun the debugger.
3:40
Make sure that that's working.
3:45
I'm gonna come over and
3:48
I'm gonna click the Get Started button and
awesome we're at our break point.
3:49
So now if you look over here,
there is this frame, so right, so
3:53
these frames are what we
were just talking about.
3:57
And you can see here that we're in our
call stack and there was quite a bit of
3:59
stuff that happened before we got to where
we're at at the top of our stack, right.
4:02
But anyways, here we are.
4:08
We're at the top of the thread stack.
4:09
And these frames
are specific to the thread.
4:10
We can switch to different threads and
see there's different frames,
4:13
different stacks of frames.
4:16
So the execution line that we are on
in this current thread, right here,
4:21
is about ready to call
a method called getCardCounts.
4:26
So let's do this.
4:29
Let's step into it and look at that.
4:30
It added a new frame for the method call.
4:33
So you can click these up and
4:36
down arrows here and you can kinda
navigate to remember how you got places.
4:37
And this will go through
all the other ones.
4:41
Here's some Java stuff,
here is some spring specific stuff.
4:42
Notice how our frames up here they're
colored white vs the ones that are color
4:46
yellow.
4:51
Now if you don't like having
all those other frames around,
4:52
you can click this filter icon here and
4:54
it will show you only or is it will
hide frames from other libraries.
4:56
It does a pretty good job of
keeping out the external noise.
5:00
When this is turned on, you should
not see code from external libraries.
5:03
Okay, so here we are in
the getCardCounts frame here and
5:07
we were passed in a request
as a parameter to the method.
5:11
So this line is declaring a new map
of long to long called cardCounts.
5:17
Sounds like some sort of illegal Las Vegas
thing, well let's see what it's doing.
5:23
So it's calling getSession on the request
which I think I'm gonna double check.
5:27
Yes it's a HTTP session.
5:33
Okay, so
what that's pulling out is the session.
5:35
And remember use cookies and
they're kinda unique for
5:37
each client, because we don't
have users we don't log in.
5:41
So it's just using a unique session
across each one of these and
5:45
it kind of works like a dictionary, right?
5:49
So, it's gonna pull off
an attribute called cardCounts.
5:52
So let's go ahead and see what it got out,
shouldn't be anything.
5:56
Okay good, right.
5:59
So cardCounts is null and
because it didn't have a key,
6:00
the session didn't have
a key called cardCounts.
6:03
So since it is null, we're gonna come in
here, we're gonna make a new hash map and
6:06
then we're gonna set a new session
attribute to cardCounts, so
6:09
we're gonna put it in there.
6:12
So this cardCounts variable seems
pretty important to the flow of
6:13
the application, right?
6:16
Now as I get older,
I keep forgetting more and more things.
6:18
So therefore I like to make sure that I
keep track of what it is that I wanna
6:21
remember.
6:24
Now basically what I'd like to
do is bookmark this variable so
6:25
I can remember it.
6:27
Thankfully for my old man use case,
there's a wonderful tool called Watches.
6:29
But before we get there,
I wanna show off one more thing.
6:33
You can actually dump the current state of
the application by clicking on this camera
6:37
down here.
6:41
You see this camera?
6:42
If I click this.
6:43
So this will look very similar to stack
traces that you log when you get an error.
6:44
It's pretty cool, right.
6:50
And you can go through
the different threads.
6:50
And sometimes when you ask
other developers for help,
6:53
like on Stack Overflow or
something, they might ask you for
6:56
the stack trace to help them further
understand the problem you're getting.
6:58
This is how you can go about sharing it.
7:01
If you can actually just copy it here
at the clipboard and you can filter and
7:02
you can choose different
things to filter through.
7:07
So what were we talking about before.
7:10
That's right, watches.
7:12
You see what I mean,
I better take a break.
7:13
I'm gonna keep everything running here so
that I don't forget.
7:15
I'll see in a bit.
7:18
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