This question is related to Why does String.valueOf(null) throw a NullPointerException?
Consider the following snippet:
public class StringValueOfNull { public static void main(String[] args) { String.valueOf(null); // programmer intention is to invoke valueOf(Object), but instead // code invokes valueOf(char[]) and throws NullPointerException } }
As explained in the answer to the linked question, Java's method overloading resolves the above invokation to String.valueOf(char[])
, which rightfully results in a NullPointerException
at run-time.
Compiled in Eclipse and javac 1.6.0_17
, this is the stack trace:
Exception in thread "main" java.lang.NullPointerException at java.lang.String.<init>(Unknown Source) at java.lang.String.valueOf(Unknown Source) at StringValueOfNull.main(StringValueOfNull.java:3)
Note that the stack trace above is missing the KEY information: it does NOT have the full signature of the valueOf
method! It just says String.valueOf(Unknown Source)
!
In most situations I've encountered, exception stack traces always have the complete signature of the methods that are actually in the stack trace, which of course is very helpful in identifying the problem immediately and a major reason why the stack trace (which needless to say is rather expensive to construct) is provided in the first place.
And yet, in this case, the stack trace does not help at all. It has failed miserably in helping the programmer identify the problem.
As is, I can see 3 ways that a programmer can identify the problem with the above snippet:
String valueOf(char[] data)
is indeed the one selectedThe last option is probably the least accessible, but of course is the Ultimate Answer (a programmer may misunderstood the overloading rule, IDE may be buggy, but bytecodes always(?) tell the truth on what's being done).
A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.
Therefore, you should log a stacktrace if, and only if, and always if, the exception indicates a bug in the program. However, that does not always indicate that a method you write should catch and log the exception.
The function printStackTrace() of the Exception class can take one parameter, either a PrintStream or a PrintWriter. Thus, it is possible, using a StringWriter, to print the stack trace into a String: StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e. printStackTrace(pw);
Note that if you are using Ant build and if the debug attribute set to false in javac command this could happen.
ex : if you need proper location in trace set debug = true in Ant build,
<javac verbose="false" srcdir="${src}" destdir="${classdir}" debug="true" includes="**/*.java"> <classpath refid="compile.classpath" /> </javac>
This is normally related to missing debug information. You are probably using JRE (not JDK), which does not include debug information for rt.jar classes. Try using full JDK, you'll get proper locations in the stack trace:
Exception in thread "main" java.lang.NullPointerException at java.lang.String.<init>(String.java:177) at java.lang.String.valueOf(String.java:2840) at StringValueOfNull.main(StringValueOfNull.java:3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With