Java

Java

Made by DeepSource

CacheLoader implementation load method should not return null JAVA-E1104

Bug risk
Major

The 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.

Bad Practice

CacheLoader myLoader = new CacheLoader<SomeKey, SomeClass>() {
    @Override
    public SomeClass load(SomeKey key) throws Exception {
        // ...

        return null; // !!!
    }
}

Recommended

Avoid returning null. Throw an appropriate exception instead.

@Override
public SomeClass load(SomeKey key) throws Exception {
    // ...

    if (cantCreateValue) throw SomeException("cause");

    return validValue;
}

References