Neatbeans Debug, track changes to a variable

Posted on 2010-09-08
Last Modified: 2013-12-14
I want to see a varaible change.  I right click and say 'add to watch' and only shows the current state it does not track its history of change?  Is there a debug tool that can do this and can work with NetBeans?
Question by:RichardSDetsch
LVL 65

Expert Comment

ID: 33632947
I have never come across a tool like that since the watch is usually to monitor the variable as you are stepping through code

why dont you try this, in the setter of that variable (assuming its a class variable), why dont you log the value being passed in.

LVL 65

Expert Comment

ID: 33632953
in case you are not familiar with the logger in Java like log4j. you do not have to worry about removing logging code then when app goes live.

Author Comment

ID: 33633035
icecream flavs:
The variable is a private data member of a class of type double and my constructor sets it, but somehow by the time it gets to the classes toString method it gets changed to 0.0.   I am unfamiliar with this operation.   So if my class is below what would I do to log the value as being passed in its setter?

public class Class{
   private double trackVar;

   public Class(double trackVar){}

    public String toString(){print trackVar}

    public setTrackVar(double d){trackVar =d}

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

LVL 65

Accepted Solution

rockiroads earned 300 total points
ID: 33633065
add the logging in setTrackVar

public setTrackVar(double d) {
    trackVar =d

if you are sure your constructor is setting it a value (which I assume is non zero) then something must be calling it. Tracing helps but you could put a breakpoint on the line trackVar = d then run your code in debug. Once it stops at that breakpoint you can check out the stack to see where it was called from

Author Comment

ID: 33633078
I am very new to this.  I will try this when I go to work tomorrow.  Thanks so much.
LVL 65

Expert Comment

ID: 33633167
No problems.

If you dont want logging at this stage, just dump it using

System.out.println("Passed in value is " + d);

LVL 65

Expert Comment

ID: 33633170
if u got time, have a look at log4j.

Expert Comment

ID: 33634712
Can we get a look on your constructor and how it is called. It may be conversion problems i.e. if you try to set an int somewhere it won't do (like double = int/int doesn't work)
LVL 10

Assisted Solution

gordon_vt02 earned 200 total points
ID: 33636871
Check your constructor and make sure you are setting this.trackVar and not just trackVar.  The 0.0 value is the default for doubles so it sounds like it isn't actually getting set in your constructor.
public Constructor(double trackVar) {
    // BAD -- Sets trackVar parameter, not class attribute
    trackVar = trackVar;

    // GOOD -- Sets trackVar class attribute to value of parameter
    this.trackVar = trackVar;

Open in new window


Author Closing Comment

ID: 33657442
These solutions are good practive.  Where you have a private variable that can only be acesses by setter functions.  But they do not track who called the setter and when.  

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 80
thymeleaf natural templating vs JSP 2 95
Java DateChooser? 3 36
object oriented programming comparison 5 70
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question