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 }
|