[fix] Java NullPointerException ComparableTimSort countRunAndMakeAscending when sorting a List


Comparable NullPointerException

If you are trying to sort a List say ArrayList of a custom class and have that class implement a Comparable interface you may run into NullPointerException with ComparableTimSort if you have a null object in your list.

Example:
import java.util.*;

/**
 *
 * Code2care.org Java Examples
 *
 * Date: 14 Jun 2022
 * Version: v1
 * Author: Code2care
 *
 */
public class ComparableExampleJava {

    public static void main(String[] args) {

        Employee e1 = new Employee(1, "Sam");
        Employee e2 = new Employee(3, "James");
        Employee e3 = new Employee(2, "Adam");

        List<Employee> empList = new ArrayList<>();
        empList.add(e1);
        empList.add(e2);
        empList.add(null);  //Null Object
        empList.add(e3);

        Collections.sort(empList);
    }
}

class Employee implements Comparable<Employee> {

   private int eid;
    private String eName;

    Employee(int eid, String eName) {
        this.eid = eid;
        this.eName = eName;
    }

    @Override
    public int compareTo(Employee employee) {
        if(employee.eid == this.eid) return 0;
        else if(employee.eid >= this.eid) return 1;
        else return -1;
    }
}
Error Stacktrace:
Exception in thread "main" java.lang.NullPointerException
	at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:321)
	at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
	at java.util.Arrays.sort(Arrays.java:1312)
	at java.util.Arrays.sort(Arrays.java:1506)
	at java.util.ArrayList.sort(ArrayList.java:1464)
	at java.util.Collections.sort(Collections.java:143)
	at EqualsExampleJava.main(ComparableExampleJava.java:26)
Fix/Solutions:

These are some work-arounds,

  • Make sure that null's are not a part of the list, you can filter them.
  • If the list is a collection of String's, you might replace null with empty string.
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency