Join Peggy Fisher for an in-depth discussion in this video Using the debugger, part of Learning Java.
- Our programs have been very simple so far and easy to find the errors, but as we start making more complex programs, it is important to know how to use a debugger. Most IDEs have built-in debugging tools. The advantaging of using this debugging tool is that it allows us to control each step in the program, and we can watch the Variables as they change. As you can see, I have our Travel Costs project file open, and it's helpful to enable the line numbering feature. If you don't see line numbers, go to View, and toggle Show Line Numbers.
Now, the first thing we want to do is add what we call a Breakpoint, which will tell the computer where we want to stop. There's two ways to do that. We can right click the number column on the far left in the editing window. So let's go ahead and do that right here at the beginning. I'm gonna right click on the line 20. I'm gonna go to Breakpoint, and Toggle Line Breakpoint. You can see the entire line is now a pinkish, reddish color, and there's a box in replace of the number on line 20.
The second way to add a Breakpoint is make sure you have the line selected where you want to stop, and go to Debug, go down to Toggle Line Breakpoint. This is also how you could remove a Breakpoint. I'm gonna put mine back on. One thing you should note is that the Breakpoints indicated where the program should stop and allow the user to manually take control of the program execution. The Breakpoint indicates the next line of code that will be executed. Once the Breakpoints have been set, click on Debug, Debug Project, or press Control F5.
Notice the IDE has added additional buttons to help you with debugging. They're located up here on the right. Stop Debugging, Pause, Continue, and then there's these five other buttons that allow me to step through the code. We will use this Step Into today. At the bottom, we see there's some new windows. There's a window for Variables, Breakpoints, and Output. As we step through the program, we can watch the Variables as their values change. When running in Debug Mode in NetBeans, it automatically stops before executing the statement.
So this green line here has not been executed yet, but it is showing me the next line that will be executed. We can continue to step through the program which allows us to execute one statement at a time, review the results, and then continue to the next statement, or use the green arrow to continue normally. Let's go ahead and click on the Step Into. Okay, the next thing it's gonna do is print the message, "Enter the total distance in miles." Let's look at the Output. Right now there's nothing there. When I click on Step Into, you'll notice, now it says, "Enter the distance in miles." I'm gonna go ahead and put 500 miles, and hit Enter.
At this point, if I go back to my Variables, what you will see is this Distance Variable will show up once I click Step Into. There it is, and it has the value over here on the right hand side of 500. I'm gonna keep going. I'm gonna Step Into, and if I go back over to Output, it's asking me for the mpg, so I'll put 35. Hit Enter. Step Into. Now it's going to ask me for the price of one gallon of gas. I'm gonna enter in the price of $3.69.
I'm gonna click Step Into, and you can see I'm here at line 28. Now it's gonna calculate the total cost by taking the distance, dividing by the mpg, and multiplying by price per gallon. Right now, total cost does not have a value, 'cause I did not execute this statement yet. Let's go look at our Variables real quick. You'll see we have distance of a 500, mpg of 35, and price per gallon of $3.69. Let's Step Into. When I Stepped Into, it now added total cost.
The total cost is $52.71. Let's go ahead and Step Into again, and one more time, and we will end our program. At the bottom, you can see it printed out the message "The trip is going to cost $52.71." These are the basics of debugging. We will continue to use the debugger as we get into more complicated programming, but this is a really powerful tool to help find logic errors. So make sure you spend some time using the debugger.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs