0

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!

3 Answers 3

4

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Alright I've implemented this, thanks alot for the shortened version! However, I'm still getting an error and I think it is because I don't understand what the Array.sort method actually does. Am I calling it correctly? Do I have to pass any elements of the array?
3

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.

2 Comments

I added an if statement to check if event1 or event2 is null. I'm still getting a nullpointer :(
Just look at the stacktrace. It complaints about a Date object being null (line 939, as the stacktrace says): kickjava.com/src/java/util/Date.java.htm
1

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;  
     }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.