Sorting using a comparable class
I'm just not sure how to approach this problem. This is the error message I'm getting:
Exception in thread "main" java.lang.NullPointerException
at java.util.Date.getMillisOf(Date.java:939)
at java.util.Date.compareTo(Date.java:959)
at FirstOccComparator.compare(FirstOccComparator.java:11)
at FirstOccComparator.compare(FirstOccComparator.java:1)
at java.util.Arrays.mergeSort(Arrays.java:1270)
at java.util.Arrays.mergeSort(Arrays.java:1281)
at java.util.Arrays.mergeSort(Arrays.java:1281)
at java.util.Arrays.mergeSort(Arrays.java:1281)
at java.util.Arrays.mergeSort(Arrays.java:1281)
at java.util.Arrays.mergeSort(Arrays.java:1281)
at java.util.Arrays.mergeSort(Arrays.java:1281)
at java.util.Arrays.mergeSort(Arrays.java:1281)
at java.util.Arrays.mergeSort(Arrays.java:1281)
at java.util.Arrays.sort(Arrays.java:1210)
at Planner.sort(Planner.java:开发者_如何学运维62)
at Test.main(Test.java:81)
Test @ line 81:
p.sort( new FirstOccComparator() );
where p is a is planner class I made.
Planner.sort @ line 62:
public void sort(Comparator<AbstractEvent> c) {
Arrays.sort(schedule, c);
}
This is my FirstOccComparator class: http://pastebin.com/4FZv4nXf (posted on pastebin because it was too wide and was hard to format here). In this class hasMoreOccurrences() return true/false if there are more reccurrences of the event. nextOccurrence() returns a Date of the next occurrence.
I'm pretty sure what I'm missing here is very simple, I'm still new at interfaces and comparator classes.
Thanks for the help!
There's a null Date object in code you haven't shared...
Just a side note, you can reduce the size of your comparator drastically. Essentially what you're doing is saying
if (x<0)
result = -1;
else if (x==0)
result = 0;
else if (x>0)
result = 1;
Why not just say result = x;
Or in your particular example:
public int compare(AbstractEvent event1, AbstractEvent event2) {
int result = 0;
if (event1.hasMoreOccurrences() && event2.hasMoreOccurrences())
result = event1.nextOccurrence().compareTo(event2.nextOccurrence());
return result;
}
Which can again be shortened (if such is your style) to one line:
public int compare(AbstractEvent event1, AbstractEvent event2) {
return (event1.hasMoreOccurrences() && event2.hasMoreOccurrences()) ? event1.nextOccurrence().compareTo(event2.nextOccurrence()) : 0;
}
Your schedule array has a null Date object?
EDIT: I mean... some "nextOccurence" returns a null, which fails on the "compareTo" of the java.util.Date class, because "compareTo" calls "java.util.Date.getMillisOf", which uses an instance variable of Date.
The nextOccurreence returned by one of the events you're camparing is null. Fix the comparator (or the AbstractEvent class) to handle the situation.
Note that the code of your comparator is more complex than it should be. You could reduce it to
import java.util.Comparator;
public class FirstOccComparator implements Comparator<AbstractEvent> {
public int compare(AbstractEvent event1, AbstractEvent event2) {
int result = 0;
if (event1.hasMoreOccurrences() && event2.hasMoreOccurrences()) {
result = event1.nextOccurrence().compareTo(event2.nextOccurrence());
}
return result;
}
}
精彩评论