Skip to content

TreeMap.get throws NPE when the key is present and any value is null #1549

@ahammel

Description

@ahammel

TreeMap.get will throw a null pointer exception when trying to do a lookup on a map with nulls in the values. HashMap works as expected under the same circumstances.

void bug() {
    final HashMap<String, String> hashMap = HashMap.of(
            "Thisworks", "fine",
            "evenwith", null);

    hashMap.get("nokey");     // Option.none()
    hashMap.get("Thisworks"); // Option.some("fine")
    hashMap.get("evenwith");  // Option.some(null)

    final TreeMap<String, String> treeMap = TreeMap.of(
            "thisbreaks", "themap",
            "lolhax", null);

    treeMap.get("nokey");       // Option.none()
    treeMap.get("thisbreaks"); // NPE
    treeMap.get("lolhax");     // NPE
}

I'm using javaslang 2.0.3

Stack trace:

java.lang.NullPointerException
    at javaslang.Tuple2.compareTo(Tuple2.java:80)
    at javaslang.Tuple2.compareTo(Tuple2.java:96)
    at javaslang.Tuple2.compareTo(Tuple2.java:28)
    at javaslang.collection.RedBlackTreeModule$Node.find(RedBlackTree.java:418)
    at javaslang.collection.TreeMap.get(TreeMap.java:420)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions