title: google guava checking for item in cache
body: <p>I'm trying to use google guava cache on a program but am not quite getting how it works. </p>
  
  <p>I'm loading up the cache and then at a later stage i'm trying to check if an item exists in the cache, my code below doesnt quite work</p>
  
  <p>The getIfPresent returns null if it doesnt exist but the load which calls it bombs out after with the error</p>
  
  <p>Exception in thread "main" com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key</p>
  
  <pre><code> private static LoadingCache&lt;String, Image&gt; imageCache
            = CacheBuilder.newBuilder()
                  .build(new CacheLoader&lt;String, Image&gt;() {
  
              @Override
              public Image load(String key) throws Exception {                    
                  if (getImage(key) != null) {                    
                      return getImage(key);                       
                  }               
                  return null;
              }                 
            });           
  
  public static Image getImage(String key) throws ExecutionException {
  
      return imageCache.getIfPresent(key);
  
  }
  </code></pre>
  
  <p>this means i cant check for the presense of the item in the cache like so</p>
  
  <pre><code>    try {
          readImage = imageCache.get(fileName);
      } catch (ExecutionException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  
      if (readImage != null) {
  
  
      }
  </code></pre>
  
  <p>can someone explain to me what i'm doing wrong here?</p>