< prev index next >

src/java.base/share/classes/java/lang/NullPointerException.java

Print this page
rev 56222 : 8218628: Add detailed message to NullPointerException describing what is null.
Summary: This is the implementation of JEP 358: Helpful NullPointerExceptions.
Reviewed-by: coleenp

@@ -68,6 +68,31 @@
      * @param   s   the detail message.
      */
     public NullPointerException(String s) {
         super(s);
     }
+
+    /**
+     * Returns the detail message string of this throwable.
+     *
+     * If no explicit message was passed to the constructor, and as
+     * long as certain internal information is available, a verbose
+     * description of the null entity is returned. After releasing the
+     * internal information, e.g., after serialization, null will be
+     * returned in this case.
+     *
+     * @return the detail message string of this {@code NullPointerException} instance
+     *         (which may be {@code null}).
+     */
+    public String getMessage() {
+        String message = super.getMessage();
+        if (message == null) {
+            return getExtendedNPEMessage();
+        }
+        return message;
+    }
+
+    // Get an extended exception message. This returns a string describing
+    // the location and cause of the exception. It returns null for
+    // exceptions where this is not applicable.
+    private native String getExtendedNPEMessage();
 }
< prev index next >