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,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- Java Date Time API: LocalDateTime get(TemporalField field) examples - Java
- How to Save Eclipse console logs in external log file - Eclipse
- Online Regex Tester Tool - Tools
- Calculate Volume of Pyramid - C-Program
- How to send SMS message in Android Emulator - Android
- tl;dr What all was announced at the Apple 12 Sept 2023 Event Wanderlust - Apple
- How to Print the Name of a Variable in Python - Python
- Installing AWS CLI Version 2 on macOS Sonoma using Terminal - AWS