Background
This question is related to Why String.valueOf (null) throws a NullPointerException?
Consider the following snippet:
public class StringValueOfNull { public static void main(String[] args) { String.valueOf(null);
As explained in the answer to a related question, overloading the Java method resolves the above involution to String.valueOf(char[]) , which rightly leads to a NullPointerException at runtime.
Compiled in Eclipse and javac 1.6.0_17 , this is a 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)
Please note that the KEY information is missing in the stack trace above: it does not have a complete signature of the valueOf method! It just says String.valueOf(Unknown Source) !
In most situations that I have encountered, exception stack traces always have a complete signature of the methods that are actually in the stack trace, which, of course, is very useful for immediately identifying the problem and the main reason why the stack trace (which, of course , quite expensive to build) is provided in the first place.
And yet in this case, stack tracing does not help at all. It failed miserably, helping the programmer identify the problem.
As is, I see three ways a programmer can identify a problem with the above snippet:
- The programmer himself understands that the method is overloaded, and according to the resolution rule in this case, an "incorrect" overload is called
- The programmer uses a good IDE that allows him / her to quickly see which method is selected
- In Eclipse, for example, when you hover over the above expression, it quickly tells the programmer that
String valueOf(char[] data) actually one
- The programmer checks the bytecode (ugh!)
The last option is probably the least accessible, but, of course, the final answer (the programmer may misunderstand the overload rule, the IDE may be a mistake, but bytecodes always (?) Tell the truth about what is being done).
Questions
- Why is the stack trace so uninformative in this case regarding the signatures of the methods that are actually in the stack trace?
- Is it because of the compiler? Lead time? Something else?
- In what other (rare?) Scenarios may the stack trace not capture important information like these?
java stack-trace exception
polygenelubricants Jun 28 '10 at 12:24 2010-06-28 12:24
source share