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