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

[lworld] Misleading exception method for assigning null to a NullRestricted field

XMLWordPrintable

      Assigning null to a NullRestricted field correctly results in an NullPointerException.
      However, the message is misleading. For example,

      `java.lang.NullPointerException: Cannot assign field "empty" because "this" is null`

      The confusing part is the reference to "this" when it should be referring to the local variable.

      The message is from JEP 358: Helpful NullPointerExceptions

      public value class Value {
              @NullRestricted
              EmptyValue empty;
              
              Value(EmptyValue empty) {
                  this.empty = empty;
              }
      }
      Invoke as
      `new Value(null)`


            fparain Frederic Parain
            rriggs Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: