You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2016/07/07 09:49:11 UTC

[jira] [Comment Edited] (GROOVY-7882) NullPointerException in List.toUnique if one element is null

    [ https://issues.apache.org/jira/browse/GROOVY-7882?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15365880#comment-15365880 ] 

Paul King edited comment on GROOVY-7882 at 7/7/16 9:48 AM:
-----------------------------------------------------------

Perhaps we should change the behavior but it is kind of doing what the javadoc says: "... items removed by using the natural ordering of the items". Calling {{compareTo}} on null might be expected to yield the kind of behavior you see. It's easy enough to supply Groovy's null-friendly spaceship operator as the comparator or as you rightly say, just return the element:
{code}
b.toUnique{ i, j -> i <=> j }
b.toUnique{ it }
{code}
It's a shame the behavior differs from {{unique}}. But from memory, there was a deliberate decision made at the time to not necessarily supply a default comparator that might slow down cases which don't need special null handling or Groovy's special number equality handling.


was (Author: paulk):
Perhaps we should change the behavior but it is kind of doing what the javadoc says: "... items removed by using the natural ordering of the items". Calling {{compareTo}} on null might be expected to yield the kind of behavior you see. It's easy enough to supply Groovy's null-friendly spaceship operator as the comparator:
{code}
b.toUnique{ i, j -> i <=> j }
{code}
It's a shame the behavior differs from {{unique}}. But from memory, there was a deliberate decision made at the time to not necessarily supply a default comparator that might slow down cases which don't need special null handling or Groovy's special number equality handling.

> NullPointerException in List.toUnique if one element is null
> ------------------------------------------------------------
>
>                 Key: GROOVY-7882
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7882
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.4.7
>         Environment: Mac OS X 10.11.5, Groovy 2.4.7 installed via sdkman
>            Reporter: Alexander Franke
>
> When using toUnique() on a List I was encountering a NullPointerException when one of the elements is null. This does not happen when using .unique() which is a quite odd behavior. So except from one being in-place and the other returning the sorted List I would expect the same result from both methods. 
> Following is the output from GroovyConsole that shows the issue.
> {code}
> groovy> List a = [1,4,2,3,3,2,1,4, null] 
> groovy> List b = [1,4,2,3,3,2,1,4, null] 
> groovy> println a.unique() 
> groovy> println a 
> groovy> println b.toUnique() 
> groovy> println b 
>  
> [1, 4, 2, 3, null]
> [1, 4, 2, 3, null]
> Exception thrown
> java.lang.NullPointerException
> 	at ConsoleScript6.run(ConsoleScript6:6)
> {code}
> Interestingly enough, using .toUnique { it } gives the proper result: 
> {code}
> groovy> List b = [1,4,2,3,3,2,1,4, null] 
> groovy> println b.toUnique { it } 
> groovy> println b 
>  
> [1, 4, 2, 3, null]
> [1, 4, 2, 3, 3, 2, 1, 4, null]
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)