title: Display default image in imageview if no image returned from server
body: <p><strong>Goal</strong></p>
  
  <p>Display a default image when no image is downloaded from server.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>I have a listview with an imageview(along with a few textbox but thats not important). 
  My imageview downloads images for students but when the student has no image I am trying to display a default image. 
  I have tried two things I thought should work, set a default image, or the code below.
  This code is taken from an activity file where I write the values from the database columns to variables (only showed img to maintain simplicity)</p>
  
  <pre><code>                   //Image path returned
                      if (javaRealObject.getString("img").equals(""))
                      {
                          imgv = (ImageView)findViewById(R.id.ivImage);
                          imgv.setImageResource(R.drawable.emptyhead);
  
                          Log.d("Test", "Empty");
                      }
                      else//No image found in column
                      {
                          student.setImage(javaRealObject.getString("img"));
                          Log.d("Test","Not Empty");
                      }  
  </code></pre>
  
  <p>However I am getting a null refernce on <code>imgv = (ImageView)findViewById(R.id.ivImage);</code> and I am not sure why since my image view is being declared. 
  Any help ti acheive the effect of a default image when none is 
  supplied from the column will be appreciated. </p>
  
  <p>For a bit more context, the code above is an activity that calls the listview.xml, which then calls the row.xml. the imageview in question is in row.xml file.</p>
  
  <p><strong>ROW.xml</strong> </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/LinearLayout1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="4dp"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content" &gt;
  
          &lt;ImageView
              android:id="@+id/ivImage"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="8dp"
              android:src="@drawable/empty_head" /&gt; //default image here
  
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical" &gt;
  
              &lt;TextView
                  android:id="@+id/tvFirstName"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="@color/primary"
                  android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
              &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>List that calls row </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;ListView
          android:id="@+id/list"
          android:layout_width="match_parent"
          android:layout_height="450dp"
          tools:listitem="@layout/row" &gt;
      &lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Adapter:</p>
  
  <pre><code>public class DriverAdapter extends ArrayAdapter&lt;Drivers&gt; {
  
      ArrayList&lt;Drivers&gt; ArrayListDrivers;
      int Resource;
      Context context;
      LayoutInflater vi;
  
      public DriverAdapter(Context context, int resource, ArrayList&lt;Drivers&gt; objects) {
          super(context, resource, objects);
  
          ArrayListDrivers = objects;
          Resource = resource;
          this.context = context;
  
          vi = (LayoutInflater) context
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          ViewHolder holder;
          if (convertView == null) {
              convertView = vi.inflate(Resource, null);
              holder = new ViewHolder();
  
              holder.imageview = (ImageView) convertView.findViewById(R.id.ivImage);
              holder.tvName = (TextView) convertView.findViewById(R.id.tvFirstName);
              holder.tvDescription = (TextView) convertView.findViewById(R.id.tvLastName);
              holder.tvClientid = (TextView) convertView.findViewById(R.id.tvid);
              holder.tvExpires = (TextView) convertView.findViewById(R.id.tv_expdate);
  
              convertView.setTag(holder);
  
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
          holder.imageview.setImageResource(R.drawable.ic_launcher);
              new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage());
  
  
          Glide.with(holder.imageview.getContext())
                  .load(new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage())        )
                  .centerCrop()
                  .placeholder(R.drawable.ic_launcher)
                  .crossFade()
                  .into(holder.imageview);
  
  
          holder.tvName.setText(ArrayListDrivers.get(position).getFirstname());
          holder.tvDescription.setText(ArrayListDrivers.get(position).getLastname());
          holder.tvClientid.setText(ArrayListDrivers.get(position).getClient_id());
          holder.tvExpires.setText("Expiry Date:"+ArrayListDrivers.get(position).getExpires());
  
  
  
          return convertView;
  
  
      }
  
      static class ViewHolder {
          public ImageView imageview;
          public TextView tvName;
          public TextView tvDescription;
          public TextView tvClientid;
          public TextView tvExpires;
  
      }
  
  
      private class DownloadImageTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
          ImageView bmImage;
  
          public DownloadImageTask(ImageView bmImage) {
              this.bmImage = bmImage;
          }
  
          protected Bitmap doInBackground(String... urls) {
              String urldisplay = urls[0];
              Bitmap cImg1 = null;
  
  
              try {
  
                  byte[] decodedString = Base64.decode(urldisplay, Base64.DEFAULT);
                  cImg1 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
  
  
              } catch (Exception e) {
                  Log.e("Error", e.getMessage());
                  e.printStackTrace();
              }
              return cImg1;
  
  
          }
  
          protected void onPostExecute(Bitmap result) {
  
              bmImage.setImageBitmap(result);
  
  
          }
      }
  }
  </code></pre>