3

I want to sort an Array of ArrayList by the first int of the ArrayLists elements. I have tried to override the compare method of the Comparator class but it throws:

Exception in thread "main" java.lang.NullPointerException
at BikeGA$1.compare(BikeGA.java:515)
at BikeGA$1.compare(BikeGA.java:1)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:351)
at java.util.TimSort.sort(TimSort.java:230)
at java.util.Arrays.sort(Arrays.java:1438)
at BikeGA.main(BikeGA.java:512)

The code is:

int max_generations = 20;
static ArrayList<Integer>[] population = new ArrayList[max_generations];

Arrays.sort(population, new Comparator<ArrayList<Integer>>(){
        @Override
        public int compare(final ArrayList<Integer> entry1, final ArrayList<Integer> entry2){
            return entry1.get(0).compareTo(entry2.get(0));
        }
    });

Can someone help me? Thanks.

5
  • 2
    Post the stack trace please. Commented Apr 27, 2015 at 7:56
  • 2
    did you initialize all ArrayLists in the array, and added at least an element to each of them? Commented Apr 27, 2015 at 7:58
  • Sounds like "entry1" and/or "entry2" is null at somepoint Commented Apr 27, 2015 at 7:58
  • 1
    Are you sure that all the ArrayLists in population are initialized? Commented Apr 27, 2015 at 7:58
  • Yes, I have printed the array before the sort and it's correctly initialized. Commented Apr 27, 2015 at 8:03

3 Answers 3

2

Your Comparator should handle null or empty ArrayLists in order for it to work with any data you put in your array :

    @Override
    public int compare(final ArrayList<Integer> entry1, final ArrayList<Integer> entry2){
        if (entry1 == null && entry2 == null)
            return 0;
        if (entry1 == null)
            return 1;
        if (entry2 == null)
            return -1;
        if (entry1.isEmpty() && entry2.isEmpty())
            return 0;
        if (entry1.isEmpty())
            return 1;
        if (entry2.isEmpty())
            return -1;
        return entry1.get(0).compareTo(entry2.get(0));
    }

This will put the null elements in the end of the array, and the empty lists before them.

Sign up to request clarification or add additional context in comments.

1 Comment

mmh don't like the fact that two null elements will be sorted in a different order depending on which one goes in the first argument. if both are null or empty you should return 0
0

You are comparing 2 arrays but you don't check if comparing object exists...

int max_generations = 20;
static ArrayList<Integer>[] population = new ArrayList[max_generations];

Arrays.sort(population, new Comparator<ArrayList<Integer>>(){
        @Override
        public int compare(final ArrayList<Integer> entry1, final ArrayList<Integer> entry2){
            Integer value1 = entry1.get(0) == null : -1 ? entry1.get(0);
            Integer value2 = entry2.get(0) == null : -1 ? entry2.get(0);

            return value1.compareTo(value2);
        }
    });

Comments

0

In my case object was:

public class Edge {
    private Integer weight;
    public Edge(Integer weight) {
        this.weight = weight;
    }
    public Integer getWeight() {
        return weight;
    }
}

And I call sort for (List edges = new ArrayList<>();) from other class:

Collections.sort(edges,(edge1,edge2) -> edge1.getWeight().compareTo(edge2.getWeight()));

Here is the result: Exception img

Solution was to initialize value of object:

public class Edge {
    private Integer weight = 0;
    public Edge(Integer weight) {
        this.weight = weight;
    }
    public Integer getWeight() {
        return weight;
    }
}

and it work!!! code 0

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.