< 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


  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 transient int numStackTracesFilledIn;
  74 
  75     /**
  76      * {@inheritDoc}
  77      */
  78     public synchronized Throwable fillInStackTrace() {
  79         numStackTracesFilledIn++;
  80         return super.fillInStackTrace();
  81     }
  82 
  83     /**
  84      * Returns the detail message string of this throwable.
  85      *
  86      * <p> If a non-null message was supplied in a constructor it is
  87      * returned. Otherwise, an implementation specific message or
  88      * {@code null} is returned.
  89      *
  90      * @implNote
  91      * If no explicit message was passed to the constructor, and as
  92      * long as certain internal information is available, a verbose
  93      * description of the null reference is returned.
  94      * The internal information is not available in deserialized
  95      * NullPointerExceptions.
  96      *
  97      * @return the detail message string, which may be {@code null}.
  98      */
  99     public String getMessage() {
 100         String message = super.getMessage();
 101         // If the stack trace was changed the extended NPE algorithm
 102         // will compute a wrong message.
 103         if (message == null && numStackTracesFilledIn == 1) {
 104             return getExtendedNPEMessage();
 105         }
 106         return message;
 107     }
 108 
 109     /**
 110      * Get an extended exception message. This returns a string describing
 111      * the location and cause of the exception. It returns null for
 112      * exceptions where this is not applicable.
 113      */
 114     private native String getExtendedNPEMessage();
 115 }
< prev index next >