
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,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- How to detect Browser and Operating System Name and Version using JavaScript - JavaScript
- iPhone Message: A new iOS update is now available. Please update from the iOS 14 beta. - Apple
- Android Studio Ctrl Shift o auto import not working - Android
- 'pwd' is not recognized as an internal or external command, operable program or batch file. [Windows] - Bash
- Formatting Double in Java [Examples] - Java
- How to Copy a remote file to local using SFTP command? - FTP
- Where is .zshrc file located in macOS - MacOS
- Convert Java Object to YAML using Jackson Library - Java