
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,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- How to Gzip a file directory on Mac OS X using Terminal Command - Mac-OS-X
- [Solved] Mic not working on iPhone 7 after iOS 14 upgrade - Apple
- Setting up Cloud feature with Notepad++ - NotepadPlusPlus
- Find Mac version using terminal command - MacOS
- Unable to connect to the Internet : Google Chrome - Chrome
- Enable Spellcheck in eclipse workspace - Eclipse
- ls command sort by file size [Linix/UNIX/macOS/bash] - Linux
- How to enable line numbers in IntelliJ - HowTos