Day96 of #100DaysOfCode

Day96 of #100DaysOfCode

Hii folks 🙌

Today I will be continuing the same pathway in which we will learn to debug with breakpoints.

Unit 4: Internet

Pathway 2: Get and Display Data

When faced with bugs that we didn’t introduce, it’s not always clear where to place log statements, or which variables to print out. Often, we can only find this information at runtime.

fun division() {
val numerator = 60
var denominator = 4
repeat(5) {
Log.v(TAG, "${numerator / denominator}")

This is where breakpoints come in! Even if we have a vague idea of what’s causing the bug based on information in the stack trace, we can add a breakpoint serving as a stop sign for a specific line of code. Once a breakpoint is reached, it will pause execution, allowing us to use other debugging tools at runtime to get an up-close look at what’s happening, and what’s gone wrong.

Attach the debugger

Behind the scenes, Android Studio uses a tool called Android Debug Bridge, also known abbreviated as ADB. This is a command-line tool that’s integrated into Android Studio and provides debugging capabilities, such as breakpoints, to the running apps. A tool for debugging is often called a debugger.

To use or attach the debugger to an app, we can’t simply run the app with Run > Run like before. Instead, we run the app with Run > Debug ‘app’.

Add breakpoints to the project

We will perform the following steps to see breakpoints in action:

  • We will add a breakpoint by clicking the gutter next to the line number we want to pause at. A dot will appear next to the line number, and the line will be highlighted.
  • Then we will the app with the debugger attached using Run > Debug ‘app’ or the

Once the app has launched, we’ll see the breakpoint highlighted when it’s activated.

At the bottom of the screen where we previously viewed the Logcat window, a new Debug tab has opened up.

On the left side is a list of functions, which are the same as the list that appeared in the stack trace. On the right side is a pane where we can check the values of individual variables in the current function (i.e. division()). At the top, there are also buttons that help us navigate our program while it's paused. The button we'll use most often is Step Over, which executes the single highlighted line of code.

Perform the following steps to debug the code:

  • Button to execute line 19. Now line 20 will be highlighted.
  1. Set a breakpoint at line 22. This is where the division occurred and is the line that the stack trace reported the exception.
  • Use the Resume Program
  • Button at the left of the Debug window to go to the next breakpoint. Run the rest of the division() function.
  • Notice that execution stops at line 17 before executing it.
  • The values of each variable numerator and denominator are shown next to their declarations. The values of the variables can be seen in the debug window in the Variables tab.
  • Press the Resume Program button on the left of the debug window four more times. Each time the loop pauses and observes the values of numerator and denominator. On the last iteration, numerator should be 60, and denominator should be 0. And we can't divide 60 by 0!

Now we know the exact line of code that causes the bug and know the exact reason. Like before, we can fix the bug by changing the number of times to repeat the code from 5 to 4.

fun division() {
val numerator = 60
var denominator = 4
repeat(4) {
Log.v(TAG, "${numerator / denominator}")

That is all for Day96 ✅

Thanks for reading, See you tomorrow!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Bootstrapping Google Kubernetes Engine after creating it

Last line of the-fence

How to make combo videos using Project Clippi

🎁 For Joining - Get 2,500 $VOXEL (~$500) ⭐ For each referral - 2,500 $VOXEL (~$500) ⭐Price Listing…

OSINT: Story of Opensource lover in Wormcon 0x01

Engineering Manager as an Architect

Deploy a Deno layer with AWS CDK and typescript

Which Data Migration Tools to use when switching CRM platforms

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Kushagra Kesav

More from Medium

Day92 of #100DaysOfCode

Day94 of #100DaysOfCode

Day42 of #100DaysOfCode

Day71 of #100DaysOfCode