< 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


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

























  73 }


  53 class NullPointerException extends RuntimeException {
  54     @java.io.Serial
  55     private static final long serialVersionUID = 5162710183389028792L;
  56 
  57     /**
  58      * Constructs a {@code NullPointerException} with no detail message.
  59      */
  60     public NullPointerException() {
  61         super();
  62     }
  63 
  64     /**
  65      * Constructs a {@code NullPointerException} with the specified
  66      * detail message.
  67      *
  68      * @param   s   the detail message.
  69      */
  70     public NullPointerException(String s) {
  71         super(s);
  72     }
  73 
  74     /**
  75      * Returns the detail message string of this throwable.
  76      *
  77      * If no explicit message was passed to the constructor, and as
  78      * long as certain internal information is available, a verbose
  79      * description of the null entity is returned. After releasing the
  80      * internal information, e.g., after serialization, null will be
  81      * returned in this case.
  82      *
  83      * @return the detail message string of this {@code NullPointerException} instance
  84      *         (which may be {@code null}).
  85      */
  86     public String getMessage() {
  87         String message = super.getMessage();
  88         if (message == null) {
  89             return getExtendedNPEMessage();
  90         }
  91         return message;
  92     }
  93 
  94     // Get an extended exception message. This returns a string describing
  95     // the location and cause of the exception. It returns null for
  96     // exceptions where this is not applicable.
  97     private native String getExtendedNPEMessage();
  98 }
< prev index next >