< prev index next >
src/java.base/share/classes/java/lang/NullPointerException.java
Print this page
rev 54075 : 8218627: Add detailed message to NullPointerException describing what is null.
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -51,15 +51,17 @@
*/
public
class NullPointerException extends RuntimeException {
private static final long serialVersionUID = 5162710183389028792L;
+ private static final String MESSAGE_PLACEHOLDER = new String();
+
/**
* Constructs a {@code NullPointerException} with no detail message.
*/
public NullPointerException() {
- super();
+ super(MESSAGE_PLACEHOLDER);
}
/**
* Constructs a {@code NullPointerException} with the specified
* detail message.
@@ -67,6 +69,33 @@
* @param s the detail message.
*/
public NullPointerException(String s) {
super(s);
}
+
+ /**
+ * Returns the detail message string of this throwable.
+ *
+ * @return the detail message string of this {@code Throwable} instance
+ * (which may be {@code null}).
+ */
+ public String getMessage() {
+ String message = super.getMessage();
+ if (message == MESSAGE_PLACEHOLDER) {
+ message = getExtendedNPEMessage();
+ setMessage(message);
+ }
+ return message;
+ }
+
+ // Install the extendedMessage in Throwable.defaultMessage before
+ // serializing.
+ private Object writeReplace() {
+ getMessage();
+ return this;
+ }
+
+ // Get an extended exception message. This returns a string describing
+ // the location and cause of the exception. It returns null for
+ // exceptions where this is not applicable.
+ private native String getExtendedNPEMessage();
}
< prev index next >