Issue Details (XML | Word | Printable)

Key: GROOVY-1956
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Paul King
Reporter: Paul King
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
groovy

null's in lists are not handled correctly for unique(), sort()

Created: 28/Jun/07 05:57 AM   Updated: 28/Jun/07 07:26 AM
Component/s: None
Affects Version/s: 1.1-beta-1
Fix Version/s: 1.1-beta-2

Time Tracking:
Not Specified


 Description  « Hide
For this list:
def x = [1, 2, 3, 1, 2, 3, null, 'a', null]

Both these throw NPE:

println x.unique()
println x.sort()


 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Michael Baehr added a comment - 28/Jun/07 06:23 AM
In DefaultGroovyMethods.java, locate the inner class NumberComparator.

Instead of
public int compare(Object o1, Object o2) {
if (o1 instanceof Number && o2 instanceof Number) {
...

check for null first:

public int compare(Object o1, Object o2) {
if (o1 == null) { return o2==null?0:-1; } else if (o2 == null) { return 1; } else if (o1 instanceof Number && o2 instanceof Number) {
...

This solves the NPE problem.


Michael Baehr added a comment - 28/Jun/07 06:27 AM
Just discovered the code tag ...

In DefaultGroovyMethods.java, locate the inner class NumberComparator.

Instead of

public int compare(Object o1, Object o2) {
  if (o1 instanceof Number && o2 instanceof Number) {
    ...

check for null first:

public int compare(Object o1, Object o2) {
  if (o1 == null) { 
    return o2==null?0:-1; 
  } else if (o2 == null) {
    return 1; 
  } else if (o1 instanceof Number && o2 instanceof Number) {
    ...

This solves the NPE problem.


Paul King added a comment - 28/Jun/07 07:26 AM
Patch and tests added.