title: NullPointerException at openFileOutput in Activity
body: <p>I have an Activity with an ImageView, which, if it is empty and clicked, should load an image from Internet and save it to internal storage. So the activity looks like this:</p>
  
  <pre><code>public class PlaceCreate extends Activity {
  Context context;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create);
    context = this;
  
    ImageView img = (ImageView) findViewById(R.id.imageView);
    img.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
          EditText edit = (EditText)findViewById(R.id.editTextName);
          Download(edit.getText().toString());
      }
    });
  }
  
  private boolean Download(String Name)
  {
    try
    {
      URL u = new URL("http://example.com/img.png");
      HttpURLConnection c = (HttpURLConnection)u.openConnection();
      c.setRequestMethod("GET");
      c.setDoOutput(true);
      c.connect();
      FileOutputStream f = context.openFileOutput(Name, Context.MODE_PRIVATE);
      InputStream in = c.getInputStream();
      byte[] buffer = new byte[1024];
      int len1 = 0;
      while((len1 = in.read(buffer)) &gt; 0 )
      {
        f.write(buffer, 0, len1);
      }
      f.close();
  
      FileInputStream is = openFileInput(Name);
      Bitmap bitmap = BitmapFactory.decodeStream(is);
      is.close();
      ImageView img = (ImageView) findViewById(R.id.imageViewPlan);
      img.setImageBitmap(bitmap);
    }
    catch(IOException e)
    {
      return false;
    }
    return true;
  }}
  </code></pre>
  
  <p>The problem is that I get NullPointerException on the line with <code>openFileOutput</code>. I found a couple of similar questions, but all answers imply that <code>null</code> is a missing context. As it's an activity in this case, it obviously has a context (<code>this</code>), and it is not null. I tried to call <code>openFileOutput</code> (with <code>this</code> implied) and via the <code>context</code> member. Either approach fails. Also I tried to pass <code>v.getContext()</code> from <code>onClick</code> into <code>Download</code> as an additional parameter, and this value was not null, yet produced the same exception as well. (If it is important, the name of the file requested is a valid filename, for example "abc".)</p>
  
  <p>Could someone shed some light on this?</p>
  
  <p>Here is an example of the stack:</p>
  
  <pre><code>04-22 00:45:10.659: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
  04-22 00:45:10.909: E/AndroidRuntime(330): FATAL EXCEPTION: main
  04-22 00:45:10.909: E/AndroidRuntime(330): java.lang.NullPointerException
  04-22 00:45:10.909: E/AndroidRuntime(330):  at android.app.ContextImpl.openFileOutput(ContextImpl.java:420)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at com.example.scanner.PlaceCreate.Download(PlaceCreate.java:93)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at com.example.scanner.PlaceCreate.access$0(PlaceCreate.java:84)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at com.example.scanner.PlaceCreate$3.onClick(PlaceCreate.java:67)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at android.view.View.performClick(View.java:2485)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at android.view.View$PerformClick.run(View.java:9080)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at android.os.Handler.handleCallback(Handler.java:587)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at android.os.Handler.dispatchMessage(Handler.java:92)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at android.os.Looper.loop(Looper.java:123)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at android.app.ActivityThread.main(ActivityThread.java:3683)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at java.lang.reflect.Method.invoke(Method.java:507)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  04-22 00:45:10.909: E/AndroidRuntime(330):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>And here is some more findings. According to the stack, the error occures inside <code>ContextImpl</code> file, and I see that <code>parent</code> local variable is null inside this method. Here is the code:</p>
  
  <pre><code>public FileOutputStream openFileOutput(String name, int mode)
    throws FileNotFoundException {
    final boolean append = (mode&amp;MODE_APPEND) != 0;
    File f = makeFilename(getFilesDir(), name);
    try {
       FileOutputStream fos = new FileOutputStream(f, append);
       setFilePermissionsFromMode(f.getPath(), mode, 0);
       return fos;
    } catch (FileNotFoundException e) {
   }
  
   File parent = f.getParentFile();
   parent.mkdir();
   FileUtils.setPermissions(
       parent.getPath(),
       FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
       -1, -1);
   FileOutputStream fos = new FileOutputStream(f, append);
   setFilePermissionsFromMode(f.getPath(), mode, 0);
   return fos;
  }
  </code></pre>
  
  <p>So now the question is how can I make sure that the call to <code>f.getParentFile()</code> will not return null, and why does it work from time to time?</p>