While trying to use a LinkedHashMap as a LRU cache, I am facing null pointer exceptions. A similar issue was discussed here, however my scenario is a bit different.
@Override
protected boolean removeEldestEntry(Map.Entry<K, CacheItem<V>> eldest)
{
if(size() >= maxEntriesOnHeap)
{
if (eldest.getValue() != null && eldest.getValue().isExpired(timeToLiveSecs))
{
offheap.put(eldest.getKey(), eldest.getValue());
}
return true;
}
return false;
}
The entry object is a wrapper object. What I found that if I do not provide the null check, it fails intermittently with the 'eldest' entry encountered having null key and null value. Proper synchronizations are in place.
So, is anyone aware of a scenario when an entry can exist with both key,value as null?