< prev index next >

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

Print this page
rev 59858 : 8248476: No helpful NullPointerException message after calling fillInStackTrace
Summary: reported by christoph.dreis@freenet.de
Reviewed-by:
rev 57039 : 8234335: Remove line break in class declaration in java.base
Summary: Remove line break in class declarations where applicable
Reviewed-by: rriggs, lancea
rev 56645 : 8218628: Add detailed message to NullPointerException describing what is null.
Summary: This is the implementation of JEP 358: Helpful NullPointerExceptions.
Reviewed-by: coleenp, clanger, rschmelter, rriggs, forax, mr
rev 56135 : 8229997: Apply java.io.Serial annotations in java.base
Reviewed-by: alanb, rriggs
rev 47216 : 8187443: Forest Consolidation: Move files to unified layout
Reviewed-by: darcy, ihse

@@ -68,10 +68,20 @@
      */
     public NullPointerException(String s) {
         super(s);
     }
 
+    private transient int numStackTracesFilledIn;
+
+    /**
+     * {@inheritDoc}
+     */
+    public synchronized Throwable fillInStackTrace() {
+        numStackTracesFilledIn++;
+        return super.fillInStackTrace();
+    }
+
     /**
      * Returns the detail message string of this throwable.
      *
      * <p> If a non-null message was supplied in a constructor it is
      * returned. Otherwise, an implementation specific message or

@@ -86,11 +96,13 @@
      *
      * @return the detail message string, which may be {@code null}.
      */
     public String getMessage() {
         String message = super.getMessage();
-        if (message == null) {
+        // If the stack trace was changed the extended NPE algorithm
+        // will compute a wrong message.
+        if (message == null && numStackTracesFilledIn == 1) {
             return getExtendedNPEMessage();
         }
         return message;
     }
 
< prev index next >