Spring boot getCache nullPointer warning false positive

SonarQube: Community Edition Version 9.4 (build 54424)
SonarLint: 6.7.0.45926

Sonar highlights following code as a major bug, linking to rule “Java:S2259”

public <T> T getCacheValue(@NonNull String cacheName, @NonNull String key, Class<T> clazz) {
    if (cacheManager.getCache(cacheName) != null) {
      return cacheManager.getCache(cacheName).get(key, clazz);
    }
    return null;
  }

cacheManager.getCache(cacheName) is null checked to avoid NullPointer, but Sonar gives allegedly false positive in this case

Technically (from the analyzer’s point of view), there is no guarantee that 2 subsequent calls to cacheManager.getCache(...) will return a non-null value.

See e.g the difference with my suggestion below:

public <T> T getCacheValue(@NonNull String cacheName, @NonNull String key, Class<T> clazz) {
    var cache = cacheManager.getCache(cacheName);
    if (cache != null) {
      return cache.get(key, clazz);
    }
    return null;
}