Duplicate :
|
A DESCRIPTION OF THE REQUEST : Null Pointer Exceptions would be much nicer if they said what object and what instance method was null when the evaluation was performed. ex: String s = null; try { System.out.println(s.length()); } catch(NullPointerException npe) { npe.printStackTrace(); } JUSTIFICATION : I understand you might have to leave off the line number, and maybe even the object field name if not running in debug mode, but not providing this information by default when you have it makes debugging a bit harder. It's particularly hard if there are multiple similiar evaluations on one line. Even a stack trace identifying the line is not much help. EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - Instead of npe.getMessage() returning the string "null", which though acurate is not helpful, it would be nice if it returned a message like this: String s was null in class MyTest.java line 220. Call to String.length() method threw a NullPointerException. ACTUAL - The string "null" is returned, which is not very helpful, or if the stack trace is printed, only the line is identified. Exception in thread "main" java.lang.NullPointerException at MyTest.main(MyTest.java:22) ---------- BEGIN SOURCE ---------- public class MyTest { public static void main(String[] args) { try { String s = "Hello Dolly"; String t = "Hello Dolly"; String u = "Hello Dolly"; String v = null; String w = null; System.out.println(s.substring(0,4) + t.substring(5,6) + u.substring(6,10) + v.length() + w.substring(6,10) ); } catch (NullPointerException npe){ System.out.println(npe.getMessage()); } } } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : Place guard code everywhere. Test everything. Assert everything. Look at the whole stack trace for the line number, go to that line number, and hope you can identify which evaluation threw the exception. ###@###.### 2004-11-11 19:36:56 GMT