Reputation: 11
I have more than 100 class files. I got an error message in one method when it return values. Example:
public String name()
{
return("John"); // error message appeared here
}
My problem is that I couldn't find out where this has been called.
Upvotes: 0
Views: 120
Reputation: 172428
You can try this:
StackTraceElement[] ste = Thread.currentThread().getStackTrace()
From Javadocs:
Returns the stack trace of the thread associated with this ThreadInfo. If no stack trace was requested for this thread info, this method will return a zero-length array. If the returned array is of non-zero length then the first element of the array represents the top of the stack, which is the most recent method invocation in the sequence. The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.
Upvotes: 0
Reputation: 17524
You may print the calling stack this way :
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
System.out.println(ste);
}
Note that most IDE have shortcuts to find where a given method is called.
For instance in Eclipse, you would select your method, then Ctrl+Shift+G will show you all possible callers of the method.
You may also use a debugger and do step-by-step check.
Upvotes: 1