load
method should not return null
JAVA-E1104The CacheLoader
interface's load
method defines the action to perform when a value that is not present in a Guava LoadingCache
is requested from the cache. LoadingCache
requires the value returned by CacheLoader.load()
to be a valid cache entry, and returning null
will cause the cache to throw an InvalidCacheLoadException
.
Ensure that an appropriate non-null value is returned instead.
CacheLoader myLoader = new CacheLoader<SomeKey, SomeClass>() {
@Override
public SomeClass load(SomeKey key) throws Exception {
// ...
return null; // !!!
}
}
Avoid returning null
. Throw an appropriate exception instead.
@Override
public SomeClass load(SomeKey key) throws Exception {
// ...
if (cantCreateValue) throw SomeException("cause");
return validValue;
}
com.google.common.cache.CacheLoader