Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8227717

Give more helpful NullPointerException messages and add flag -XX:ShowCodeDetailsInExceptionMessages

    XMLWordPrintable

    Details

    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      If someone checks the message of NullPointerExceptions for null, he might now encounter a String. Nevertheless, there are already lots of NPEs that have messages, i.e. return a String.
    • Interface Kind:
      add/remove/modify command line option, Other
    • Scope:
      Implementation

      Description

      Summary

      Add new messages to NullPointerExceptions that precisely specify the location of the exception in code.

      Add a manageable flag called -XX:ShowCodeDetailsInExceptionMessages to configure the content of exception messages as above new message.

      Problem

      NullPointerExceptions do not clearly specify where the exception occurred, i.e., if there are several object references dereferenced in one line of code, the current empty message along with the line number does not specify which of these references is null.

      Exception messages could contain snippets of (static) code to give details of the problem causing the exception. Some users may not want to expose such code snippets in exception messages.

      Solution

      Add messages to NullPointerExceptions raised in a line of code as a.b.c.i = 99; that tell where the exception occurred, as Cannot assign field 'i' because 'a.b.c' is null.

      To configure messages that contain snippets of code as a.b.c, add a manageable flag called -XX:ShowCodeDetailsInExceptionMessages.

      Specification

      The new NullPointerException message is described in detail in JEP 358 (https://openjdk.java.net/jeps/358). It improves messages of NPEs thrown by the Java Virtual Machine when it fails to execute a bytecode because it needs to dereference a null reference. When the exception is raised by the JVM's runtime, the bytecode that failed to execute is known. Looking at the previously executed bytecodes, the access path leading to the null reference is reconstructed and the message text is generated. Whether this message is printed can be configured with ShowCodeDetailsInExceptionMessages.

      ShowCodeDetailsInExceptionMessages is a boolean flag. If it is false, messages containing snippets of code as in above example are not printed. If the flag is true, the messages are printed. true is the intended long-term default value, but it was agreed to switch the NullPointerException message off, first. Thus, the default first will be false.

      The flag's help message is "Show exception messages from RuntimeExceptions that contain snippets of the failing code. Disable this to improve privacy."

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              goetz Goetz Lindenmaier
              Reporter:
              goetz Goetz Lindenmaier
              Reviewed By:
              Coleen Phillimore
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: