title: Android Application crashes as getting image from URL to bitmap to ImageView
body: <p>I know that this is a big beginner's problem and that it has to do with the correct number of threads and that you can't have too much going on in the UI thread, but I'm not exactly sure where this error is. </p>
  
  <p>Here is my main (and only) activity's Java code :</p>
  
  <pre><code>   package com.example.bitmapdisplay;
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  
  import android.R;
  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.drawable.BitmapDrawable;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.widget.ImageView;
  
  public class MainActivity extends Activity {
  
      Bitmap bitmap;
      ImageView image;
      BitmapDrawable bd;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(com.example.bitmapdisplay.R.layout.activity_main);
  
          image = (ImageView) findViewById(com.example.bitmapdisplay.R.id.ivImage);
  
  
      }
  
      protected void onStart() {
          ImageDownloader load = new ImageDownloader(image);
          load.execute("http://i1.cpcache.com/product_zoom/617914535/dickbutt_2_mug.jpg?side=Back&amp;height=250&amp;width=250&amp;padToSquare=true");
          image.setImageBitmap(bitmap);
      }
  
  
  
      public class ImageDownloader extends AsyncTask &lt;String, Void, Bitmap&gt; {
  
          ImageView image;
          public ImageDownloader(ImageView view) {
  
              image = view;
              view = null;
          }
  
          @Override
          protected Bitmap doInBackground(String... url) {
  
              String url2 = url[0];
  
              Bitmap bitmap = null;
  
              byte[] imageData = getImagefromURL(url2);
              ByteArrayInputStream inputStream = new ByteArrayInputStream(imageData);
              bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(inputStream), 250, 250, true);
              return bitmap;
          }
  
          protected void onPostExecute(Bitmap bitmap) {
              image.setImageBitmap(bitmap);
          }
  
  
      }
  
      public byte[] getImagefromURL(String url) {
  
          InputStream in = null;
          byte[] byteimage = null;
  
          try {
              URL imageUrl = new URL(url);
              URLConnection conn = imageUrl.openConnection();
              HttpURLConnection httpConn = (HttpURLConnection) conn;
              httpConn.setAllowUserInteraction(false);
              httpConn.setInstanceFollowRedirects(true);
              httpConn.setRequestMethod("GET");
              httpConn.connect();
  
              int response = httpConn.getResponseCode();
  
              if(response == HttpURLConnection.HTTP_OK) {
                  in = httpConn.getInputStream();
              }
  
              int nRead;
              byte[] data = new byte[16384];
              ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  
              while((nRead = in.read(data,0,data.length)) != -1) {
                  buffer.write(data,0,nRead);
              }
  
              buffer.flush();
  
              byteimage = buffer.toByteArray();
  
          }
          catch(IOException e) {
              e.printStackTrace();
          }
          return byteimage;
      }
  
  }
  </code></pre>
  
  <p>All of the XML should be in place. However, did I maybe mess up the threads? (I'm new to this).</p>
  
  <p>Here is the logcat: </p>
  
  <pre><code>05-28 22:17:37.450: D/AndroidRuntime(844): Shutting down VM
  05-28 22:17:37.450: W/dalvikvm(844): threadid=1: thread exiting with uncaught exception (group=0xb1ab8ba8)
  05-28 22:17:37.640: E/AndroidRuntime(844): FATAL EXCEPTION: main
  05-28 22:17:37.640: E/AndroidRuntime(844): Process: com.example.bitmapdisplay, PID: 844
  05-28 22:17:37.640: E/AndroidRuntime(844): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bitmapdisplay/com.example.bitmapdisplay.MainActivity}: java.lang.NullPointerException
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.os.Handler.dispatchMessage(Handler.java:102)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.os.Looper.loop(Looper.java:136)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.app.ActivityThread.main(ActivityThread.java:5017)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at java.lang.reflect.Method.invokeNative(Native Method)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at java.lang.reflect.Method.invoke(Method.java:515)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at dalvik.system.NativeStart.main(Native Method)
  05-28 22:17:37.640: E/AndroidRuntime(844): Caused by: java.lang.NullPointerException
  05-28 22:17:37.640: E/AndroidRuntime(844):  at com.example.bitmapdisplay.MainActivity.onStart(MainActivity.java:41)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.app.Activity.performStart(Activity.java:5241)
  05-28 22:17:37.640: E/AndroidRuntime(844):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
  05-28 22:17:37.640: E/AndroidRuntime(844):  ... 11 more
  </code></pre>