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.
Asked
Active
Viewed 1,819 times
2
-
What IDE/editor are you using? – Rajesh J Advani Aug 18 '12 at 16:55
-
possible duplicate of [data breakpoints in java/eclipse](http://stackoverflow.com/questions/4086039/data-breakpoints-in-java-eclipse) – Tomasz Nurkiewicz Aug 18 '12 at 16:56
3 Answers
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:
- 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)
- Show Breakpoints View by Window menu -> Show View -> Other... Breakpoints
- Click the variable you toggled, right click and uncheck access option. To keep modification only.
- Run app in debug mode.
You should be able to watch the variable's modification.

Gene Wu
- 386
- 3
- 4