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

Calling collect on null doesn't result in a NPE

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Information Provided
    • 2.3.7
    • None
    • groovy-runtime
    • None

    Description

      def someCollection = null
      
      someCollection.collect { e -> e.name } // returns []
      someCollection*.name                   // returns null (or [] for SC)
      someCollection.name                    // throws NullPointerException
      

      Actual:
      someCollection is null, with no typing hints as to what someCollection really is, and yet it invokes .collect() on a null reference. How is that possible without collect being a global function?

      Expected:
      All 3 situations throw a NPE. The spread operator seems to short circuit nulls like the ?. operator. So really the spread operator is more like ?.* operator which is maybe desirable. But it's not explicit that it does that and is a bit surprising given we have the difference of . and ?. At the least this is inconsistent between these various operations.

      Attachments

        Activity

          People

            emilles Eric Milles
            chubbard Charlie Hubbard
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: