< prev index next >

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

Print this page
rev 54124 : 8218628: Add detailed message to NullPointerException describing what is null.

*** 1,7 **** /* ! * Copyright (c) 1994, 2011, 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 --- 1,7 ---- /* ! * 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
*** 67,72 **** --- 67,97 ---- * @param s the detail message. */ public NullPointerException(String s) { super(s); } + + /** + * Returns the detail message string of this throwable. + * + * If no explicit message was passed to the constructor, and as + * long as certain internal information is available, a verbose + * description of the null entity is returned. After releasing the + * internal information, e.g., after serialization, null will be + * returned in this case. + * + * @return the detail message string of this {@code NullPointerException} instance + * (which may be {@code null}). + */ + public String getMessage() { + String message = super.getMessage(); + if (message == null) { + return getExtendedNPEMessage(); + } + return message; + } + + // 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 >