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

Comparison operators throw NPE, contrary to docs

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-beta-3
    • 1.0-beta-3
    • None
    • None

    Description

      According to this page (http://groovy.codehaus.org/operators.html), the comparison operators handle nulls gracefully and avoid throwing NullPointerException. However, this only seems to be the case for ==, ===, and !=

      For example:

      groovy> "dog" > null
      groovy>
      Caught: java.lang.NullPointerException
      java.lang.NullPointerException
      at java.lang.String.compareTo(String.java:727)
      at java.lang.String.compareTo(String.java:777)
      at org.codehaus.groovy.runtime.Invoker.compareTo(Invoker.java:284)
      at org.codehaus.groovy.runtime.InvokerHelper.compareGreaterThan(InvokerHelper.java:237)
      at CommandLine18.run(CommandLine18.groovy)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:236)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:215)
      at groovy.ui.InteractiveShell.run(InteractiveShell.java:100)
      at groovy.ui.InteractiveShell.main(InteractiveShell.java:71)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.codehaus.classworlds.Launcher.launchStandard(Launcher.java:410)
      at org.codehaus.classworlds.Launcher.launch(Launcher.java:344)
      at org.codehaus.classworlds.Launcher.main(Launcher.java:474)

      groovy> null <= 3
      groovy>
      Caught: groovy.lang.GroovyRuntimeException: Cannot compare values: null and right
      groovy.lang.GroovyRuntimeException: Cannot compare values: null and right
      at org.codehaus.groovy.runtime.Invoker.compareTo(Invoker.java:288)
      at org.codehaus.groovy.runtime.InvokerHelper.compareLessThanEqual(InvokerHelper.java:233)
      at CommandLine19.run(CommandLine19.groovy)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:236)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:215)
      at groovy.ui.InteractiveShell.run(InteractiveShell.java:100)
      at groovy.ui.InteractiveShell.main(InteractiveShell.java:71)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.codehaus.classworlds.Launcher.launchStandard(Launcher.java:410)
      at org.codehaus.classworlds.Launcher.launch(Launcher.java:344)
      at org.codehaus.classworlds.Launcher.main(Launcher.java:474)

      Attachments

        Activity

          People

            Unassigned Unassigned
            rodcope Rod Cope
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: