From 05147c625c28beba8a556bcc3122f5e8979d563e Mon Sep 17 00:00:00 2001 From: Stephan Preibisch Date: Fri, 24 Mar 2023 15:22:20 -0400 Subject: [PATCH] fix an annoying bug that when regularly calling invalidateAll() on a SoftRefCache, it throws a NullPointerException. Somehow "ref" can be null, check line 213 while get() is still being called --- .../java/net/imglib2/cache/ref/SoftRefLoaderCache.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/imglib2/cache/ref/SoftRefLoaderCache.java b/src/main/java/net/imglib2/cache/ref/SoftRefLoaderCache.java index 4e3d562..2ca66b6 100644 --- a/src/main/java/net/imglib2/cache/ref/SoftRefLoaderCache.java +++ b/src/main/java/net/imglib2/cache/ref/SoftRefLoaderCache.java @@ -82,7 +82,15 @@ public Entry( final K key ) public V getValue() { - return ref.get(); + // instead of synchronize statement + final CacheSoftReference< V > myRef = ref; + if ( myRef == null ) + return null; + else + return myRef.get(); + + // old code (Problem: myRef can be null when calling invalidateAll() + // return myRef.get(); } public void setValue( final V value )