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

Add detailed message to NullPointerException describing what is null.

    XMLWordPrintable

    Details

    • Subcomponent:
    • Understanding:
      Fix Understood

      Description

      When getting a NPE it is often hard to determine which reference in
      an expression had been null. This change adds a message telling
      this. Some examples:

      boolean[] za1 = null;
      za1[0]
      "'za1[0]' is null. Can not load from null byte/boolean array."

      za1.length
      "'za1' is null. Can not read the array length."

      fa1[0] = 0
      "'fa1[0]' is null. Can not store to null float array."

      nullInstanceField.nullInstanceField
      "'this.nullInstanceField' is null. Can not read field 'nullInstanceField'."

      throw null;
      "'null' is null. Can not throw a null exception object."

      nullInstanceField.testCreationViaNew()
      "'this.nullInstanceField' is null. Can not invoke method 'void NullPointerExceptionTest.testCreationViaNew()'."

      synchronized (nullInstanceField)
      "'this.nullInstanceField' is null. Can not enter a null monitor."

      The message differs whether the class file was compiled
      with or without debug information.

      With debug info, the local variable and parameter names are listed.
      Without debug information, <local1>, <local2>, <parameter1> etc is printed.

      For more details see the test included in the change that
      contains a lot of example messages.

      The message is computed by parsing the bytecodes. The starting point to parse is derived from the top stack frame. Computing the message is some overhead. As NullPointerExceptions are thrown frequently, but most of the time the message is not needed, we compute the message only if it is requested.

      To know that we should compute the message lazily this patch adds a field to the exception indicating whether the message still needs to be computed.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                goetz Goetz Lindenmaier
                Reporter:
                goetz Goetz Lindenmaier
              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated: