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

NPE when using safe traversal operator with CompileStatic

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.5
    • 2.4.6
    • Compiler
    • None

    Description

      In the code below, the "if" condition throws a NPE when it reaches the "p3" object. If I remove the @CompileStatic from the "SomeOther" class it works.

      import groovy.transform.CompileStatic
      
      @CompileStatic
      class Person {
        String name
        OtherInfo otherInfo
      }
      
      @CompileStatic
      class OtherInfo {
        int favoriteNumber
      }
      
      @CompileStatic
      class SomeOther {
        SomeOther() {
          Person p1 = new Person(name: 'Ben', otherInfo: new OtherInfo(favoriteNumber: 1))
          Person p2 = new Person(name: 'Aaron', otherInfo: new OtherInfo())
          Person p3 = new Person(name: 'Fred')
          [p1,p2,p3].each { Person p ->
            println "checking: ${p.name}"
            println p?.otherInfo?.favoriteNumber
            if(p?.otherInfo?.favoriteNumber == 7){
              println "luckiest number!"
            }
          }
        }
      }
      
      new SomeOther()
      

      Attachments

        Activity

          People

            shils Shil Sinha
            podollb Ben Podoll
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: