title: NullPointerException when accessing OutputStream from DiskLruCache
body: <p>I am using <code>DiskLruCache</code> to cache urls of images. I download an image using <code>AsyncTask</code> like so:</p>
  
  <pre><code>new AsyncTask&lt;String, String, String&gt;() {
  
          String path = null;
  
          @Override
          protected String doInBackground(String... params) {
  
              // Get the assets from DiskLruCache
              try {
                  if (url != null)
                      path = assetManager.getAssetFilePath(url);
              } catch (IOException e) {
                  callback.downloadFailed();
              }
  
              return path;
          }
  
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              if (path != null)
                  callback.assetDownloaded(path);
              else
                  callback.downloadFailed();
          }
  
      }.execute();
  </code></pre>
  
  <p><code>AssetManager</code> is a custom helper class that calls a method <code>getAssetFilePath(...)</code>. The exception occurs here in in this piece of code:</p>
  
  <pre><code>    DiskLruCache.Snapshot snapshot = mDiskCache.get(key);
      if (snapshot == null) {
  
          DiskLruCache.Editor editor = mDiskCache.edit(key);
  
          InputStream is = downloadFile(url);
  
          if (is == null) return null;
  
          // EXCEPTION OCCURS HERE
          OutputStream os = editor.newOutputStream(0);
  
          byte[] buffer = new byte[1024 * 10];
          int len;
          while ((len = is.read(buffer)) != -1) {
              os.write(buffer, 0, len);
          }
  
          is.close();
          os.close();
  
          editor.commit();
      }
  </code></pre>
  
  <p>Specifically this line here causes the exception:</p>
  
  <pre><code>OutputStream os = editor.newOutputStream(0);
  </code></pre>
  
  <p>I can't seem to duplicate this issue on any of my test devices but it is occurring for other users. </p>
  
  <p>What is the cause of this issue and how can I go about tackling it?</p>
  
  <p>The logcat is telling me:</p>
  
  <blockquote>
    <p>Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream com.jakewharton.disklrucache.DiskLruCache$Editor.newOutputStream(int)' on a null object reference</p>
  </blockquote>
  
  <p>Is there a better way to do this?</p>
  
  <p>Also is this a good way to initialize <code>DiskLruCache</code>? If not then what is a good scalable initialization? Looking for best practices and implementations.</p>
  
  <pre><code>public CacheManager() {
  
      BASE_PATH = Environment.getExternalStorageDirectory().getPath() + "/exampleapp";
  
      try {
          mDiskCache = DiskLruCache.open(new File(BASE_PATH), VERSION, 1, CACHE_SIZE);
      } catch (IOException e) {
          Log.i(TAG, "Asset Manager IOException");
          e.printStackTrace();
      }
  
  }
  </code></pre>