title: Null Pointer exception in ImageView
body: <p>I am trying to display an image from my phone using ImageView.
  Here is my ImageActivity activity:</p>
  
  <pre><code>package com.example.apptest1;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.Bundle;
  import android.view.View; 
  import android.widget.ImageView;
  
  public class ImageActivity extends Activity {
  
  private static final int REQUEST_CODE = 0;
  private Bitmap bitmap;
  private ImageView imageView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_image);
       ImageView imageView = (ImageView) findViewById(R.id.result);
  
  }
  
  public void pickImage(View View) {
      Intent intent = new Intent();
      intent.setType("image/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      intent.addCategory(Intent.CATEGORY_OPENABLE);
      startActivityForResult(intent, REQUEST_CODE);
  }
  
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          InputStream stream = null;
          if (requestCode == REQUEST_CODE &amp;&amp; resultCode == Activity.RESULT_OK)
            try {
              // recyle unused bitmaps
              if (bitmap != null) {
                bitmap.recycle();
              }
              stream = getContentResolver().openInputStream(data.getData());
              bitmap = BitmapFactory.decodeStream(stream);
  
              imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
              e.printStackTrace();
            } finally {
              if (stream != null)
                try {
                  stream.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
            }
        }
  
  
  
   }
  </code></pre>
  
  <p>And here is the activity_image.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="${relativePackage}.${activityClass}" 
  android:orientation="vertical"&gt;
  
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="pickImage"
      android:text="Button" &gt;
  &lt;/Button&gt;
  
  &lt;ImageView
      android:id="@+id/result"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
       &gt;
  &lt;/ImageView&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>But when I try to run the application,it displays the below logcat and stops abruptly
  LogCat:</p>
  
  <pre><code>    05-28 23:11:48.226: E/AndroidRuntime(6115): FATAL EXCEPTION: main
  05-28 23:11:48.226: E/AndroidRuntime(6115): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,  request=0, result=-1, data=Intent { dat=content://media/external/images/media/1242 flg=0x1 }} to activity   {com.example.apptest1/com.example.apptest1.ImageActivity}: java.lang.NullPointerException
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3525)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3568)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.access$1100(ActivityThread.java:162)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.os.Handler.dispatchMessage(Handler.java:107)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.os.Looper.loop(Looper.java:194)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.main(ActivityThread.java:5371)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at java.lang.reflect.Method.invoke(Method.java:525)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run   (ZygoteInit.java:833)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at dalvik.system.NativeStart.main(Native Method)
  05-28 23:11:48.226: E/AndroidRuntime(6115): Caused by: java.lang.NullPointerException
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at com.example.apptest1.ImageActivity.onActivityResult(ImageActivity.java:48)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.Activity.dispatchActivityResult(Activity.java:5311)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3521)
  05-28 23:11:48.226: E/AndroidRuntime(6115):     ... 11 more
  </code></pre>
  
  <p>I have debuged and checked that ImageView is null but not able to figure out why/how.Can someone please clear my doubt.Thanks in advance</p>