< prev index next >

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

Print this page
rev 59899 : 8248476: No helpful NullPointerException message after calling fillInStackTrace
Summary: reported by christoph.dreis@freenet.de
Reviewed-by:


  53     @java.io.Serial
  54     private static final long serialVersionUID = 5162710183389028792L;
  55 
  56     /**
  57      * Constructs a {@code NullPointerException} with no detail message.
  58      */
  59     public NullPointerException() {
  60         super();
  61     }
  62 
  63     /**
  64      * Constructs a {@code NullPointerException} with the specified
  65      * detail message.
  66      *
  67      * @param   s   the detail message.
  68      */
  69     public NullPointerException(String s) {
  70         super(s);
  71     }
  72 





















  73     /**
  74      * Returns the detail message string of this throwable.
  75      *
  76      * <p> If a non-null message was supplied in a constructor it is
  77      * returned. Otherwise, an implementation specific message or
  78      * {@code null} is returned.
  79      *
  80      * @implNote
  81      * If no explicit message was passed to the constructor, and as
  82      * long as certain internal information is available, a verbose
  83      * description of the null reference is returned.
  84      * The internal information is not available in deserialized
  85      * NullPointerExceptions.
  86      *
  87      * @return the detail message string, which may be {@code null}.
  88      */
  89     public String getMessage() {
  90         String message = super.getMessage();
  91         if (message == null) {
  92             return getExtendedNPEMessage();









  93         }
  94         return message;
  95     }
  96 
  97     /**
  98      * Get an extended exception message. This returns a string describing
  99      * the location and cause of the exception. It returns null for
 100      * exceptions where this is not applicable.
 101      */
 102     private native String getExtendedNPEMessage();






 103 }


  53     @java.io.Serial
  54     private static final long serialVersionUID = 5162710183389028792L;
  55 
  56     /**
  57      * Constructs a {@code NullPointerException} with no detail message.
  58      */
  59     public NullPointerException() {
  60         super();
  61     }
  62 
  63     /**
  64      * Constructs a {@code NullPointerException} with the specified
  65      * detail message.
  66      *
  67      * @param   s   the detail message.
  68      */
  69     public NullPointerException(String s) {
  70         super(s);
  71     }
  72 
  73     private static final String mustComputeExtendedNPEMessage = "1";
  74     private static final String noExtendedNPEMessage = "2";
  75     private volatile transient String extendedMessage;
  76 
  77     /**
  78      * {@inheritDoc}
  79      */
  80     public synchronized Throwable fillInStackTrace() {
  81         // After the stack trace is changed the extended NPE algorithm
  82         // will compute a wrong message. So compute it beforehand.
  83         if (extendedMessage == null) {
  84             // The first call. Don't compute the message, we'll
  85             // do it lazily.
  86             extendedMessage = mustComputeExtendedNPEMessage;
  87         } else if (extendedMessage == mustComputeExtendedNPEMessage) {
  88             // The second call.
  89             extendedMessage = getExtendedNPEMessage();
  90         }
  91         return super.fillInStackTrace();
  92     }
  93 
  94     /**
  95      * Returns the detail message string of this throwable.
  96      *
  97      * <p> If a non-null message was supplied in a constructor it is
  98      * returned. Otherwise, an implementation specific message or
  99      * {@code null} is returned.
 100      *
 101      * @implNote
 102      * If no explicit message was passed to the constructor, and as
 103      * long as certain internal information is available, a verbose
 104      * description of the null reference is returned.
 105      * The internal information is not available in deserialized
 106      * NullPointerExceptions.
 107      *
 108      * @return the detail message string, which may be {@code null}.
 109      */
 110     public String getMessage() {
 111         String message = super.getMessage();
 112         if (message == null) {
 113             synchronized(this) {
 114                 if (extendedMessage == mustComputeExtendedNPEMessage) {
 115                     // Only the original stack trace was filled in. Message will
 116                     // compute correctly.
 117                     extendedMessage = getExtendedNPEMessage();
 118                 }
 119                 if (extendedMessage != noExtendedNPEMessage) {
 120                     return extendedMessage;
 121                 }
 122             }
 123         }
 124         return message;
 125     }
 126 
 127     /**
 128      * Get an extended exception message. This returns a string describing
 129      * the location and cause of the exception. It returns noExtendedNPEMessage for
 130      * exceptions where this is not applicable.
 131      */
 132     private String getExtendedNPEMessage() {
 133         String msg = getExtendedNPEMessage0();
 134         if (msg == null) return noExtendedNPEMessage;
 135         return msg;
 136     }
 137 
 138     private native String getExtendedNPEMessage0();
 139 }
< prev index next >