javaautoboxingmapdb

NPE while autoboxing in Java


I have the following piece of code:

map =  db_.treeMapCreate(validMapName_)
          .keySerializer(Serializer.LONG)
          .valueSerializer(Serializer.LONG)
          .make(); //mapDB
protected void onTaskCompletion(TaskInfo task)
{
   long startBlk = task.blkId;
   long count = task.count;
   for (int i=0; i < count; ++i)
   {
      long blk = startBlk + i;
      Long oldVal = map.get(blk); //NPE here
      ...
      ...
   }
}

How is it possible to get an NPE while autoboxing? I can understand getting an NPE on unboxing i.e. if I had:

long oldVal = map.get(blk)

then that can throw an NPE.

Edit: Map isn't null. To be more specific, inside the BTreeMap code of mapDB, this line gets executed:

if(key==null) throw new NullPointerException();

In case someone wants to take a look: BtreeMap of mapDB Line: 1024

Partial stacktrace:

java.lang.NullPointerException
        at org.mapdb.BTreeMap.get(BTreeMap.java:1024)
        at org.mapdb.BTreeMap.get(BTreeMap.java:1013)
        at com.service.onTaskCompletion(TaskHandler.java:312)

I'm unable to reproduce it so I cannot give a minimal, verifiable example. I have tried to run it so that I perform a get() for a key that doesn't exist, and it DOESN'T give an NPE.


Solution

  • I think the problem is that you are looking at the wrong version of the BTreeMap code. If you go back to the previous commit of this class, line 1024 is not:

    if(key==null) throw new NullPointerException();
    

    but:

    while(!A.isLeaf()) {...}
    

    where A is set by:

    BNode A = engine.get(current, nodeSerializer);
    

    This makes much more sense. Basically, null is returned from engine.get. How that is possible is beyond my understanding, but this could very well be a bug of mapdb itself.