Netbeans/C2/Netbeans-Debugger/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Netbeans Debugger

Author: Sindhu

Keywords: netbeans, debugging, debug


Resources for "NetBeans Debugger"


Visual Cue
Narration
Slide 1 Hello everyone.


Welcome to the tutorial on Netbeans Debugger.

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,


and Netbeans IDE v7.1.1

Slide 4 & 5

Netbeans Debugger

We all know that debugging programs can be a rather painstaking task.


Hence, knowing a debugging tool and being familiar with its features can help you save valuable time.


This powerful debugging tool is very useful,


especially when you have to code or test large programs.


In this tutorial we will learn some of the features that the Netbeans Debugger provides.

Slide 6 & 7

Lesson Outline

This tutorial will acquaint you with
  • the debugging window
  • configuring breakpoints
  • evaluating expressions (setting watches)
  • options for tracing the execution of your program
  • options to configure the debugger itself


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.


To set a breakpoint, click on the line number.


I will set at this line which prints Hello World!

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,


by clicking on the Debug Project button in the toolbar,


the execution of the program stops at the line at which the breakpoint is located.

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.


So far, only the variable 'a' has been initialized.

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


In the Source editor, highlight the object 'aSample; in the code

Before proceeding, let us see how to add a watch.


For example, let us say I want to watch on the integer value 'aSample'.

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'.


Click on OK.


So far 'aSample' has not been created so it says it does not know the value.


Once it executes the line we'll know what the variable contains.

Evaluating Expressions


In the workspace >> highlight the expression b=a+10

In a similar way you can also watch and evaluate expressions.


Here I'm checking for b=a+10.


What if I wanted to know what a-4 is.

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.


Here I enter the expression 'a-4'.


Click on the Evaluate Expression button here, and in the Variable window, it says 'a-4's value is 6.

Step-Over


Point to the line 'System.out.println("Hello World! a is " + a);' >> click on Step-Over button below toolbar

Let's now proceed and execute this single line of code.


To do that, choose the Step-Over button from the toolbar.

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”.


To see the output, go to the output window and choose the sampleDebug output. That says, Hello World! a is 10.

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.


To that I can choose the Step Into option from the toolbar.

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.


When I Step Over again, we can see that this.variable is now set to 10.

Step-Out


From the toolbar >> click on Step-Out option

To get out of this function I can either choose Continue, Step Over or Step Out.


Let me choose Step-Out to come out of the method.


And now I'm back to where the function call was made.

From the toolbar >> click on Step-Over option


Re-focus Variable window >> point to aSample.value >> value is set to 10

When I say Step-Over again, you will notice that aSample.value is now set to10.


This is what we were watching for.

Run to Cursor


Place cursor near the line >> d=b-5;


Apart from Breakpoints and StepOvers, you can also stop the execution of the program at the line of the cursor.


For example, let me go into the function here and set the cursor to be on this line which says d=b-5; .

From the toolbar >> click on Run to Cursor option Now from the toolbar, choose the Run To Cursor option.


You will notice that the execution of the program gets into the function and stops at the line where the cursor is located.

Show execution upto line


Re-focus the Variable window >> point to the variable b >> value is set to 20

You can see that it has computed the value of b, as 20.


And inside the variable window, it has set 'b' to be 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.


We can now check it in the variable window or hover on the variable to check it's value.

Finish Debugger Session


Point to Finish Debugger Session option in toolbar

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.


Let me choose Continue here.

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.

  1. Set breakpoints in the code. Usually, you would set a break at the calling point of a function which you suspect has the error.
  2. Use Step-Into to go into the function.
  3. Use Step-Overs to execute the lines and make sure to inspect the values of variables in the variable window.
  4. Add some watches to help you identify and correct the error.
  5. Step-Out of the method.
  6. Continue till you reach the next breakpoint.
  7. Finish the debugger session and Run your application.


Slide 10 & 11 Recap.

In this tutorial, we became familiar with the netbeans debugger.

  1. We saw how to set breakpoints and watches.
  2. Add expressions which we want to evaluate, while the code is running.
  3. Trace execution of a program with Step-Into, Step-Over, Step-Out and Run-to-Cursor options.
  4. Also saw how to configure the debugger for advanced debugging.

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.


It summarizes the Spoken Tutorial project.


If you do not have good bandwidth, you can download and watch it.

Slide 13

Spoken Tutorial Workshops

The Spoken Tutorial Project Team
  • Conducts workshops using Spoken Tutorials
  • Gives certificates to those who pass an online test
  • For more details, contact us at contact@spoken-tutorial.org



Slide 14

Acknowledgements

Spoken Tutorial Project is a part of the Talk to a Teacher Project.


It is supported by the National Mission on Education through ICT, MHRD, Government of India.


More information on this Mission is available at http://spoken-tutorial.org/NMEICT-Intro

Slide 15 About the Contributor

This tutorial has been contributed from IT for Change

Thank you for joining us.

Contributors and Content Editors

Chandrika