2

I am working on SWT based GUI application. I have one object variable which is private. this variable becomes null at some time in the run time of application. i want to know how to set a watchdog kind of debug so that i will come to know which thread is making it null.

Favonius
  • 13,959
  • 3
  • 55
  • 95
Sumant
  • 496
  • 1
  • 6
  • 20

3 Answers3

5

The easiest and IDE-independent way to do it is to encapsulate your variable behind a setter / getter pair, and add a statement to your code watching for setting the value null:

Was:

public MyObject obj;

Change to:

private MyObject obj;
public MyObject getObj() {
    return obj;
}
public void setObj(MyObject val) {
    if (val == null) {
        // Add a log debug statement, and set a breakpoint here
    }
    obj = val;
}

Encapsulating your variables is generally a good thing that usually proves useful outside of debugging context as well.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

1. I think you are talking about an Object Reference Variable of certain type of object.

In its setter method check for value.

Eg:

CustomObject cObject;

public void setObject(CustomObject o){


       if(o.equals(null)){


              // Save it in log, Sysout it... etc.
       }
        else{

              cObject = o;

         }

 }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

I suppose you are using Eclipse to debug your App.

For Eclipse, you can set up a break point for access and modification, here is the quick steps:

  1. Focus the line of private variable. Select Run menu -> Toggle Break Point. (if you are using default key mapping, you could press Ctrl+Shift+B)
  2. Show Breakpoints View by Window menu -> Show View -> Other... Breakpoints
  3. Click the variable you toggled, right click and uncheck access option. To keep modification only.
  4. Run app in debug mode.

You should be able to watch the variable's modification.

Gene Wu
  • 386
  • 3
  • 4