Getting inexplicable NullPointerException from get on empty LoadingCache

2,054 views
Skip to first unread message

svk...@gmail.com

unread,
Jun 9, 2017, 6:59:13 PM6/9/17
to guava-discuss
Hi All,

I created a CacheLoader

    CacheLoader<CommutativePointPair,Double> cacheLoader = new CacheLoader<CommutativePointPair,Double>() {
  public Double load(CommutativePointPair pp) {
  return distance(new Point(pp.a1,pp.a2), new Point(pp.b1,pp.b2))
                }
  }
which I used to setup a LoadingCache

    LoadingCache<CommutativePointPair,Double>  cache = CacheBuilder.newBuilder().maximumSize(10000).build(cacheLoader);

The cache starts out empty and on the very first fetch from the cache (from within multi-threaded code)..

         CommutativePointPair pointPair = new CommutativePointPair(p1,p2)
 log.info("Entries in cache: ${distanceByPointsCache.size()}") // returns Zero
         cache.getUnchecked(pointPair)

I'm getting a NPE (Excerpt below)

java.lang.NullPointerException
at com.google.common.cache.LocalCache$Segment.drainKeyReferenceQueue(LocalCache.java:2549)
at com.google.common.cache.LocalCache$Segment.drainReferenceQueues(LocalCache.java:2538)
at com.google.common.cache.LocalCache$Segment.runLockedCleanup(LocalCache.java:3509)
at com.google.common.cache.LocalCache$Segment.preWriteCleanup(LocalCache.java:3490)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2257)
at com.google.common.cache.LocalCache.get(LocalCache.java:4000)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4004)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4874)
at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4880)
        ...

I noticed in the debugger that control goes to lockedGetOrLoad method because internal variable count is 0.
I've tried adding .expireAfterWrite(100, TimeUnit.MINUTES) to the cache-builder-configuration but it didn't make any difference.

Is there anything obvious that I am missing? I'm using guava14.0.1 (which was selected by code written previously).

Would greatly appreciate any help, pointers.

Thanks
Vish

Louis Wasserman

unread,
Jun 9, 2017, 7:04:41 PM6/9/17
to svk...@gmail.com, guava-discuss
Guava is now on version 22; version 14 is over four years old, it looks like, now.  I'd definitely see if upgrading your version of Guava fixes the issue, there have certainly been bugfixes to common.cache in that period.

--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/9fe97c39-1dfd-4ed6-8770-b3968a4b6130%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

svk...@gmail.com

unread,
Jun 12, 2017, 1:46:49 PM6/12/17
to guava-discuss, svk...@gmail.com
Hi Louis,

Thanks much for your response. I took your advice and switched to Guava 20.0 which is the last version that supports jdk1.7 (I understand versions higher than that require jdk8 but I need to stick with jdk1.7).
However, even with guava 20.0 I still get the same kind of NPE with the stack trace starting at the same function in guava:

Caused by: java.lang.NullPointerException
at com.google.common.cache.LocalCache$Segment.drainKeyReferenceQueue(LocalCache.java:2519)
at com.google.common.cache.LocalCache$Segment.drainReferenceQueues(LocalCache.java:2508)
at com.google.common.cache.LocalCache$Segment.runLockedCleanup(LocalCache.java:3550)
at com.google.common.cache.LocalCache$Segment.preWriteCleanup(LocalCache.java:3531)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2232)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2208)
at com.google.common.cache.LocalCache.get(LocalCache.java:4053)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4057)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4986)
at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4992)
        ...

Must be is something I am not doing right, but what?

Thanks
Vish

svk...@gmail.com

unread,
Jun 12, 2017, 4:24:36 PM6/12/17
to guava-discuss, svk...@gmail.com
OK, I think I have solved this problem.  It had to do with the fact that I had wrapped my cache in an object and that object was being deep-cloned by some other code without my knowledge.
The retrieval of data was being attempted on this cloned cache which doesn't work like it should on the original cache.
Once I disabled the cloning, the original (non-cloned) reference to the cache was used to do the 'get' and all worked well.

Hope this lesson will save someone the time and effort. Thanks to Louis Wasserman for the help.

Thanks
Vish
Reply all
Reply to author
Forward
0 new messages