Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8200

Shorthand |= results in NPE

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.11
    • 2.5.0-beta-2
    • None
    • None
    • Groovy Version: 2.3.11 JVM: 1.7.0_80 Vendor: Oracle Corporation OS: Linux

      java version "1.7.0_80"
      Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

    Description

      I've stumbled across some curious behaviour for the shorthand assignment operator in Groovy v2.3.11 (symptom also present in Groovy v2.4.9).

      The following boolean or logic, unexpectedly throws NullPointerException.

      boolean x = null
      
      // short hand unrolled:  x = null || x
      
      x |= null // -> unexpected NullPointerException
      
      assert !x

      The same approach for boolean and logic, performs as expected.

      boolean x = null
      
      // short hand unrolled:  x = null && x
      
      x &= null
      
      assert !x

      It's possible to work around the issue with explicit cast.

      x |= null as boolean

      Attachments

        1. shorthand_and.groovy
          0.2 kB
          Daniel Hammer
        2. shorthand_or_symptom.groovy
          0.2 kB
          Daniel Hammer
        3. shorthand_xor_symptom.groovy
          0.2 kB
          Daniel Hammer

        Issue Links

          Activity

            People

              paulk Paul King
              dleh Daniel Hammer
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: