< prev index next >

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

Print this page
rev 56353 : 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


  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      * <p> If a non-null message was supplied in a constructor it is
  78      * returned. Otherwise, an implementation specific message or
  79      * {@code null} is returned.
  80      *
  81      * @implNote
  82      * If no explicit message was passed to the constructor, and as
  83      * long as certain internal information is available, a verbose
  84      * description of the null reference is returned.
  85      * The internal information is not available in deserialized
  86      * NullPointerExceptions.
  87      *
  88      * @return the detail message string, which may be {@code null}.
  89      */
  90     public String getMessage() {
  91         String message = super.getMessage();
  92         if (message == null) {
  93             return getExtendedNPEMessage();
  94         }
  95         return message;
  96     }
  97 
  98     /**
  99      * Get an extended exception message. This returns a string describing
 100      * the location and cause of the exception. It returns null for
 101      * exceptions where this is not applicable.
 102      */
 103     private native String getExtendedNPEMessage();
 104 }
< prev index next >