Netbeans/C2/Netbeans-Debugger/English
Title of script: Netbeans Debugger
Author: Sindhu
Keywords: netbeans, debugging, debug
Resources for "NetBeans Debugger"
|
|
Slide 1 | Hello everyone.
|
Slide 2
Introduction |
If this is the first time you are using Netbeans, please view the earlier tutorials on the Spoken Tutorial website. |
Slide 3
System Setup |
For this demonstration, I am using the Linux Operating System Ubuntu v12.04,
|
Slide 4 & 5
Netbeans Debugger |
We all know that debugging programs can be a rather painstaking task.
|
Slide 6 & 7
Lesson Outline |
This tutorial will acquaint you with
|
Switch to Netbeans IDE | Now Let's get started and debug this sample code. I will switch to the Netbeans IDE |
In the Workspace >> show the project sampleDebug | I have already created a Java Application, sampleDebug in my IDE for this demonstration. |
In the Workspace >> highlight to show the variables a,b & c | This is a small program that initializes three integer values a, b, and c. |
Highlight the line 'System.out.println("Hello World! a is " + a);' | Then it prints 'Hello World!' and the value of 'a'. |
Highlight the lines in the code snippet containing the class object and the line where the integer variable value is declared | It also creates a class object 'SampleClass', which has a 'value' integer, as a private integer value. |
Highlight the line in the code containing the expression b=a+10 | Then, it computes the value of 'b', |
Highlight the line in the code containing the expression c=getc(b); | and calls a function to compute the value of c, |
Highlight the line in the code containing the statement 'System.out.println("b is "+b+" and c is "+c);' | and prints the values of 'b' and 'c'. |
Within the Source >> click on the line number containing the line
'System.out.println("Hello World! a is " + a);' |
To start with the debugging, let us first set the breakpoint.
|
Point to the small pink square in the line number column | Notice that the line at which a breakpoint is set changes it's colour to pink and is marked by a small square against its line number. |
Point to the highlighted line where the execution has stopped | When you run the program in the debugging mode,
|
Hover the cursor over the variable a, to view it's value | So far, 'a's value has been set.
Hover on it to check it's value. It indicates that it's value is 10.
|
You can see that there are some additional windows below the workspace. | |
Open the Variables window below the workspace >> Point to the row containing the variable a | There is a 'Variables' window that shows a list of variables and their values.
|
Re-focus the Output window below the workspace | We can also look at the 'Output' window with the sample debug output.
There is no output yet. |
Re-focus the Debugger window below the workspace >> Highlight the line saying ' program hit a breakpoint on line 29' | There is also a 'Debugger Console' that says that the program hit a breakpoint on line 29 and has stopped there. |
Re-focus the Breakpoints window below the workspace
|
There is also a 'Breakpoints' window that tells you that a breakpoint has been set on line number 29.
|
Adding Watchpoint
|
Before proceeding, let us see how to add a watch.
|
Re-focus the Variables window below the workspace >> double click to <Enter the watch> >> Enter a.Sample.value >> Click on OK | In the 'Variables' window below the workspace, I will double-click on <Enter the Watch> option and enter the name of the variable 'aSample.value'.
|
Evaluating Expressions
|
In a similar way you can also watch and evaluate expressions.
|
Go to the Debug menu >> select Evaluate Expression | So let me go to the Debug menu in the menu bar, and select Evaluate expression option. |
Point to the Evaluate Code window >> Enter a-4 >> Click on the green evaluate button on the right side | The 'Evaluate Code' window appears in the workspace.
|
Step-Over
|
Let's now proceed and execute this single line of code.
|
Re-focus Output window >> show printed line >> Hello World! a is 10 | That should execute only that 1 single line (current execution line) of the code to print “Hello World”.
|
Step-Into | The program has now stopped (at the line) to create a SampleClass object. |
From the toolbar >> click on Step-Into option | Now, I want to go into the constructor of the SampleClass.
|
From the toolbar >> click on Step-Over option | Then I can choose Step Over and see that the value came inside the constructor call is now set to 10. |
Move and hover the cursor over the variable val >> this.val is set to 10 | I can also check that by hovering on the variable.
|
Step-Out
|
To get out of this function I can either choose Continue, Step Over or Step Out.
|
From the toolbar >> click on Step-Over option
|
When I say Step-Over again, you will notice that aSample.value is now set to10.
|
Run to Cursor
|
Apart from Breakpoints and StepOvers, you can also stop the execution of the program at the line of the cursor.
|
From the toolbar >> click on Run to Cursor option | Now from the toolbar, choose the Run To Cursor option.
|
Show execution upto line
|
You can see that it has computed the value of b, as 20.
|
Re-focus the Variable window >> point to the variable d >> value is set to 15 | Now, I can choose Step Over again and d's value also gets initialized and becomes 15. |
Now, I can either choose to return or completely finish the execution of the program. | |
From the toolbar >> click on Step-Out option | Let me choose Step Out and come back to the function call. |
Move cursor over getC() >> value is 15 | When you hover on the getC() function, you'll notice that the function has (returns) a value of 15. |
(variable) 'c' has not yet been assigned that value. | |
Re-focus the Variable window >> point to the variable c >> value is set to 15 | So, when we Step Over and execute that line, 'c' will get a value of 15.
|
Finish Debugger Session
|
Now if you want to stop the debugging session, you can choose the Finish Debugger Session option from the toolbar. |
Point to Continue option in toolbar | If you want to continue (the execution) to the next breakpoint you can choose the Continue option. |
Click on Continue option | Once you finish, you can also choose the Continue option to complete the execution of the (remaining) program.
|
Re-focus output window to show output >> b is 20 and c is 15 | In the Output window, it shows me the output as: b is 20 and c is 15. |
Now, this was a quick overview of the options of debugging on netbeans. | |
Tools > Options > Miscellaneous > Java Debugger | If you want any advanced feature settings, you can -
Go to Tools menu, click on Options, go to Miscellaneous option, click on the Java Debugger tab. Here you can change settings for multi-threaded program breakpoint (options). Or have filters to decide on which methods you would want to step in. |
Slide 8 & 9
Assignment |
Now to the assignment.
As an assignment, take any of your programs, excellent if it has already errors. If not, introduce some errors with the logic or algorithm.
|
Slide 10 & 11 | Recap.
In this tutorial, we became familiar with the netbeans debugger.
Hope this tutorial saves you a lot of time in your testing and debugging tasks. |
Slide 12
About the Spoken Tutorial Project
|
Watch the video at the link shown on the screen.
|
Slide 13
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
|
Slide 14
Acknowledgements |
Spoken Tutorial Project is a part of the Talk to a Teacher Project.
|
Slide 15 | About the Contributor
This tutorial has been contributed from IT for Change Thank you for joining us. |