Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Unknown Source) in Exception stack trace
    primarykey
    data
    text
    <h3>Background</h3> <blockquote> <p><em>This question is related to</em> <a href="https://stackoverflow.com/questions/3131865/why-does-string-valueofnull-throw-a-nullpointerexception">Why does String.valueOf(null) throw a NullPointerException?</a></p> </blockquote> <p>Consider the following snippet:</p> <pre><code>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 } } </code></pre> <p>As explained in the answer to the linked question, Java's method overloading resolves the above invokation to <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#valueOf%28char%5B%5D%29" rel="noreferrer"><code>String.valueOf(char[])</code></a>, which rightfully results in a <code>NullPointerException</code> at run-time.</p> <p>Compiled in Eclipse and <code>javac 1.6.0_17</code>, this is the stack trace:</p> <pre><code>Exception in thread "main" java.lang.NullPointerException at java.lang.String.&lt;init&gt;(Unknown Source) at java.lang.String.valueOf(Unknown Source) at StringValueOfNull.main(StringValueOfNull.java:3) </code></pre> <p>Note that the stack trace above is missing the <em>KEY</em> information: it does <em>NOT</em> have the full signature of the <code>valueOf</code> method! It just says <code>String.valueOf(Unknown Source)</code>!</p> <p>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 <em>very helpful</em> 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.</p> <p>And yet, in this case, the stack trace <em>does not help at all</em>. It has failed miserably in helping the programmer identify the problem.</p> <p>As is, I can see 3 ways that a programmer can identify the problem with the above snippet:</p> <ul> <li>Programmer realizes on his/her own that the method is overloaded, and by resolution rule, the "wrong" overload gets invoked in this case</li> <li>Programmer uses a good IDE that allows him/her to quickly see which method is selected <ul> <li>In Eclipse, for example, mouse-hovering on the above expression quickly tells programmer that the <code>String valueOf(char[] data)</code> is indeed the one selected</li> </ul></li> <li>Programmer examines the bytecode (ugh!)</li> </ul> <p>The 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).</p> <hr> <h3>The questions</h3> <ul> <li>Why is the stack trace so uninformative in this case with regards to the signatures of the methods that are actually in the stack trace? <ul> <li>Is this due to the compiler? The runtime? Something else?</li> </ul></li> <li>In what other (rare?) scenarios can the stack trace fail to capture essential information like these?</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload