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,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Program 4: Input Name and Age and Print - 1000+ Python Programs - Python-Programs
- Java SE JDBC: Insert with PreparedStatement Example - Java
- How to change the name of your Mac running on macOS Ventura 13 - MacOS
- Bash Hello World! Script Tutorial - Bash
- AppleScript Example with TextEdit - MacOS
- Notepad++ Keyboard Shortcuts Cheatsheet - Shortcuts
- How to Clear StringJoiner in Java 8 - Java
- Android Display Toast on Button Click - Android