math_law
math_law

Reputation: 391

Java display the actual Java code line for a line in StackTrace

Assume I am getting the following java Exception

ERROR - Error occured while reading properties. (MyPropertyReader.java) 
    java.lang.NullPointerException
        at com.x.MyPropertyReader.init(PropertyReader.java:**222**)
        at com.x.MyPropertyReader.<init>(PropertyReader.java:119)

How do you find out , what excatly the code block looks like at line 222 ?

For example: The above exception is due to the following line of code and I want to see the much like below line in log files.

propertyConfiguration.getProperty("REPORT_INFLUENCE_THRESHOLD"))).trim());

I want to see REPORT_INFLUENCE_THRESHOLD string in the log files. However I am curious in general what how to programmatically print line in 222 during execution.

Upvotes: 2

Views: 183

Answers (1)

fge
fge

Reputation: 121710

You first need, obviously, to have the source files in your classpath. This or at least at a location you know you can find them.

Then you would use Throwable's .getStackTrace() which returns an array of StackTraceElements.

The latter has the source file name and line (IF AVAILABLE; a jar may, or may not, have been compiled with debug information); from then on, read, find line, print!

Upvotes: 2

Related Questions