Skip to content

Null pointer exception thrown when calling floorKey method on BTreeMap with a key greater than the highest key stored in the Map #85

Closed
vimil opened this Issue Jun 21, 2012 · 1 comment

2 participants

@vimil
vimil commented Jun 21, 2012

A null pointer exception is thrown when getFloorKey method of BTreeMap is called with a key that is greater than the highest key stored in the BTreeMap. the stacktrace and testcase are as follows.

public static void main(String[] args) {
    TreeMap<Long, String> treeMap = new TreeMap<Long, String>();
    treeMap.put(1L, "hello");
    System.out.println(treeMap.lastKey());
    System.out.println(treeMap.floorKey(2L)); // No exception thrown by TreeMap prints 1

    DB testDB = DBMaker.openFile("D:/temp/testData").disableTransactions().setMRUCacheSize(10000).make();
    NavigableMap<Long, String> testMap = testDB.<Long, String> createTreeMap("testMap");
    testMap.put(1L, "hello");
    System.out.println(testMap.lastKey());
    System.out.println(testMap.floorKey(2L)); // throws Nullpointer exception, Stack trace is as follwos
}

StackTrace
Exception in thread "main" java.lang.NullPointerException
at org.apache.jdbm.Utils$1.compare(Utils.java:50)
at org.apache.jdbm.Utils$1.compare(Utils.java:1)
at org.apache.jdbm.BTreeMap.floorKey(BTreeMap.java:401)
at MappedFileBufferTest.main(MappedFileBufferTest.java:29)

@vimil
vimil commented Jul 14, 2012

Patch to fix the issue

diff --git a/src/main/java/org/apache/jdbm/Utils.java b/src/main/java/org/apache/jdbm/Utils.java
index de374bb..cb06352 100644
--- a/src/main/java/org/apache/jdbm/Utils.java
+++ b/src/main/java/org/apache/jdbm/Utils.java
@@ -47,7 +47,7 @@
*/
static final Comparator COMPARABLE_COMPARATOR = new Comparator() {
public int compare(Comparable o1, Comparable o2) {

  • return o1.compareTo(o2);
  • return o1 == null && o2 != null ? -1 : (o1 != null && o2 == null ? 1 : o1.compareTo(o2)); } };
@jankotek jankotek added a commit that referenced this issue Aug 21, 2012
@jankotek Fix NPE in issue #85 f7de667
@jankotek jankotek closed this Aug 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.