
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.
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- How to do screen recording on Mac - MacOS
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree - Python
- Fix: FileNotFoundError: [Errno 2] No such file or directory: Python - Python
- Convert GMT/UTC timezone in Excel to EST, CST, MST, PST, AST and HST - Microsoft
- Set Title to Android AlertDialog - Android
- How to Split a String using Rust Language - Rust
- Docker Run Command Examples - Part 1 - Docker
- Unhandled exception type InterruptedException : Java Threads - Java