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

Unexpected NullPointerException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.0-beta-1
    • 2.3.0-beta-2
    • None
    • None

    Description

      While upgrading Grails to use Groovy 2.3.0-beta-1 I came across a scenario where a NullPointerException is being thrown in a scenario where I don't think it should be. It seems that the null safety operator isn't working as it should.

      I have attached a simple sample app with the following code. Running the test yields a NullPointerException.

      ./gradlew test
      
      src/main/groovy/com/demo/Widget.groovy
      package com.demo
      
      @groovy.transform.CompileStatic
      class Widget {
          def isValid(Map m) {
              boolean isValid = true
              def dir = m.dir?.toString()
              def file = m.file?.toString()
      
              if(file) {
                  if (!(file.startsWith('/') || dir?.endsWith('/'))) {
                      isValid = false
                  }
              }
      
              isValid
          }
      }
      
      src/test/groovy/com/demo/WidgetSpec.groovy
      package com.demo
      
      import spock.lang.Specification
      
      class WidgetSpec extends Specification {
          void 'test with file and no dir'() {
              when:
              def widget = new Widget()
              def m = [file: 'someFile']
      
              then:
              !widget.isValid(m)
          }
      }
      

      Attachments

        1. nullproblem.zip
          51 kB
          Jeff Brown

        Activity

          People

            blackdrag Jochen Theodorou
            brownj Jeff Brown
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: