Reputation: 10016
...
BREAKPOINT `int i = 10 + 12;`
...
When I add a break point to code in Android Studio as above, AS will run to the line I added the break point to, as expected. However, if there is no code after the break point line I can't see what i
resolves to in the debugger. I can fix this problem by inserting a single line of dummy code, but there must be an easier/more elegant way to get around this problem that I'm missing.
How can I avoid adding a dummy line of code to view these values?
EDIT: I'm using the latest version of Android Studio (1.0.2)
Upvotes: 2
Views: 2139
Reputation: 15685
It's possible to put a breakpoint on the line following the last line you want executed.
A breakpoint stops execution before the line under the breakpoint is executed.
In the example above, a new variable is introduced (i
), which is as yet unknown to the interpreter. It's necessary to step-over (execute) the line of code in order for this variable to be initialized.
See the example below, in which there's a test function that contains the line of code from the question:
I've put breakpoints on the start brace of the function {
, the line in question, and the end brace }
. Note that there's an X on the first breakpoint, as it's invalid. The current execution point is on (or just before execution of) the dark-blue highlited line. Note that the variable i
does not exist in the Variables window below.
When I step over (execute) the line of code, the active line is now over the final brace in the function }
, and that the variable i
is now initialized and set:
So, if you're interested in breaking after a given line of code, just put the breakpoint after that line of code, even if that line is the final brace of a function.
Note: this won't work if there is an exit statement (e.g. break
, return
etc.) as the final executed line of code in a function.
Upvotes: 2