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

@CompileStatic + Elvis operator cause null pointers to be thrown

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.0
    • 2.3.2, 2.2.3
    • Static compilation
    • None

    Description

      When using @CompileStatic and an elvis operator unexpected NullPointerExceptions are thrown. Here is an example script to recreate the issue.

      package my.company
      @Grab(group='org.codehaus.groovy', module='groovy-all', version='2.1.6')
      import groovy.transform.CompileStatic
      
      @CompileStatic
      class Outer {
      	
      	public static void main(String[] args) {
      		Inner inner
      		int someInt = inner?.somestuff ?: 0
      		println someInt
      	}
      
      	class Inner {
      		int somestuff
      	}
      }
      

      You would expect this to print 0 always, but if @CompileStatic is used a NPE is thrown. Without @CompileStatic 0 is printed as expected.

      Attachments

        Activity

          People

            melix Cédric Champeau
            aarmistead Aaron Armistead
            Votes:
            6 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: