title: How to solve android.content.res.Resources$NotFoundException: Resource ID #0x0 & properly display data from a database to a cardview?
body: <p>I am trying to build a card view that displays a list of data from my SQLite database. I use <code>grocery_item</code> class to create the recycler view &amp; take the data from <code>MyDataBaseHelper</code> &amp; put the data into ArrayList. In the same class, I then create an object of <code>captionedImagesAdapter</code> to create the view holder for the card view and bind the data from the ArrayLists to the Card Views.</p>
  <p>However, whenever I try to run the program I receive the exception <code>android.content.res.Resources$NotFoundException: Resource ID #0x0</code> &amp; the card views are not displayed on the <code>item_grocery.xml</code> layout. The line of code that is causing the problem is <code>at com.myapps.myapplication.captionedImagesAdapter.onBindViewHolder(captionedImagesAdapter.java:44)</code> which is <code>Drawable drawable = ContextCompat.getDrawable (holder.cardView.getContext(), images [position]);</code> &amp;
  <code>at com.myapps.myapplication.captionedImagesAdapter.onBindViewHolder(captionedImagesAdapter.java:14)</code> which is <code>public class captionedImagesAdapter extends RecyclerView.Adapter &lt;captionedImagesAdapter.ViewHolder&gt;</code>.</p>
  <p>I have tried solving the problem by check if the ArrayList had null references but everything is fine and from the research, I have done the exception is caused because the resource cannot be found from <code>holder.cardView.getContext()</code>. However, I am stuck on this issue I don't know if I am displaying data from a database to a Recycler view the right way.</p>
  <p>Here is the code for my <code>grocery_item.java</code>activity:</p>
  <pre><code>package com.myapps.myapplication;
  
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteException;
  import android.os.Bundle;
  import android.util.Log;
  import java.util.ArrayList;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.recyclerview.widget.GridLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  
  public class grocery_item extends AppCompatActivity {
  
  ArrayList &lt;String&gt; groceryName;
  ArrayList &lt;Integer&gt; groceryImages;
  SQLiteDatabase db;
  Cursor cursor;
  
  protected void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.item_grocery);
  
      RecyclerView groceryRecycler = (RecyclerView) findViewById(R.id.grocery_recycler_view);
  
      groceryName = new ArrayList&lt;String&gt;();
      groceryImages = new ArrayList &lt;Integer&gt;();
  
      accessDataBase();
  
      cursor = db.query(&quot;ITEMS&quot;, new String[] {&quot;NAME&quot;, &quot;DESCRIPTION&quot;, &quot;IMAGE_RESOURCE_ID&quot;}, null, null, null, null, null);
  
      for (int i = 0; i &lt;= 11; i++) {
  
          if (i == 0) {
              if (cursor.moveToFirst()) {
                  groceryName.add (cursor.getString (0));
                  groceryImages.add (cursor.getInt (1));
              }
  
          } 
  
          if (i != 0) {
              if (cursor.moveToNext()) {
                  groceryName.add (cursor.getString (0));
                  groceryImages.add (cursor.getInt (1));
              } else {
                  Log.d (&quot;blue&quot;, &quot;the next record in the cursor was not found for the images&quot;);
              }
          }
  
      }
  
      captionedImagesAdapter adapter = new captionedImagesAdapter (groceryName, groceryImages);
  
      groceryRecycler.setHasFixedSize(true);
      GridLayoutManager layoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
      groceryRecycler.setLayoutManager(layoutManager);
      groceryRecycler.setAdapter (adapter);
  }
  
  public void accessDataBase () {
      MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this);
      try {
          db = databaseHelper.getReadableDatabase();
  
          if (db != null) {
              Log.d (&quot;db&quot;, &quot;db is is accessed&quot;);
          }
      } catch (SQLiteException e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  <p>Here is the code for my <code>item_grocery.xml</code>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.recyclerview.widget.RecyclerView
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:id=&quot;@+id/grocery_recycler_view&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:scrollbars=&quot;vertical&quot; /&gt;
  </code></pre>
  <p>Here is code for my <code>card_view.xml</code>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.cardview.widget.CardView
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:card_view=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:id=&quot;@+id/card_view&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;200dp&quot;
      android:layout_margin=&quot;5dp&quot;
      card_view:cardElevation=&quot;2dp&quot;
      card_view:cardCornerRadius=&quot;4dp&quot;&gt;
  
  &lt;LinearLayout
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:orientation=&quot;vertical&quot; &gt;
  
      &lt;ImageView
          android:id=&quot;@+id/info_image&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;0dp&quot;
          android:layout_weight=&quot;1.0&quot;
          android:scaleType=&quot;centerCrop&quot; &gt;
  
      &lt;/ImageView&gt;
  
      &lt;TextView
          android:id=&quot;@+id/info_text&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginLeft=&quot;4dp&quot;
          android:layout_marginBottom=&quot;4dp&quot; &gt;
  
      &lt;/TextView&gt;
  &lt;/LinearLayout&gt;
  &lt;/androidx.cardview.widget.CardView&gt;
  </code></pre>
  <p>Here is the code for my <code>captionedImagesAdapter.java</code>:</p>
  <pre><code>package com.myapps.myapplication;
  
  import android.graphics.drawable.Drawable;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  import java.util.ArrayList;
  import androidx.core.content.ContextCompat;
  import androidx.recyclerview.widget.RecyclerView;
  import androidx.cardview.widget.CardView;
  
  public class captionedImagesAdapter extends RecyclerView.Adapter &lt;captionedImagesAdapter.ViewHolder&gt; {
  
  private int [] images;
  private String [] names;
  
  public captionedImagesAdapter (ArrayList &lt;String&gt; captions, ArrayList &lt;Integer&gt; imageIds) {
  
      images = new int [11];
      names = new String [11];
  
      for (int i = 0; i &lt;11; i++) {
          images [i] = imageIds.get (i);
      }
  
      for (int i = 0; i &lt; 11; i++) {
          names [i] = captions.get (i);
      }
  }
  
  public captionedImagesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      CardView cv = (CardView) LayoutInflater.from (parent.getContext()).
              inflate(R.layout.card_view, parent, false);
  
      return new ViewHolder (cv);
  }
  
  public void onBindViewHolder(ViewHolder holder, int position) {
      Drawable drawable = ContextCompat.getDrawable (holder.cardView.getContext(), images [position]);
      holder.imageView.setImageDrawable (drawable);
      holder.textView.setText (names [position]);
  }
  
  public int getItemCount() {
      Log.d (&quot;captionSize&quot;, String.valueOf(names.length));
      return names.length;
  }
  
  public static class ViewHolder extends RecyclerView.ViewHolder {
      private ImageView imageView;
      private TextView textView;
      private CardView cardView;
  
      public ViewHolder(CardView view) {
          super(view);
          imageView = view.findViewById(R.id.info_image);
          textView = view.findViewById(R.id.info_text);
          cardView = view;
      }
  }
  }
  </code></pre>
  <p>Here is the code for my SQLiteHelper:</p>
  <pre><code>package com.myapps.myapplication;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  public class MyDatabaseHelper extends SQLiteOpenHelper {
  
  private static final String DB_NAME = &quot;starbuzz&quot;;
  private static final int DB_VERSION = 1;
  
  public MyDatabaseHelper(Context context) {
      super(context, DB_NAME, null, DB_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      db.execSQL(&quot;CREATE TABLE ITEMS (&quot; + &quot;_id INTEGER PRIMARY KEY AUTOINCREMENT, &quot; + &quot;NAME TEXT, &quot; + &quot;DESCRIPTION TEXT, &quot; + &quot;IMAGE_RESOURCE_ID INTEGER) ;&quot;);
      insertItems (db, &quot;Faan&quot;, &quot;Bangla Faan&quot;, R.drawable.faan);
      insertItems (db, &quot;Milk&quot;, &quot;Whole Milk&quot;, R.drawable.milk);
      insertItems (db, &quot;Egg&quot;, &quot;A tray of Eggs&quot;, R.drawable.egg);
      insertItems (db, &quot;Toilet Tissue&quot;, &quot;A bag of toilet tissue&quot;, R.drawable.toilet_tissue);
      insertItems (db, &quot;Kitchen Tissue&quot;, &quot;A bag of kitchen tissue&quot;, R.drawable.kitchen_tissue);
      insertItems (db, &quot;Bread&quot;, &quot;a bag of bread&quot;, R.drawable.bread);
      insertItems (db, &quot;Potatoe&quot;, &quot;a sack of potatoe&quot;, R.drawable.potatoe);
      insertItems (db, &quot;Onion&quot;, &quot;a sack of onions&quot;, R.drawable.onion);
      insertItems (db, &quot;Flour&quot;, &quot;a packet of flour&quot;, R.drawable.flour);
      insertItems (db, &quot;Tomatoe&quot;, &quot;a packet of tomatoe&quot;, R.drawable.tomatoe);
      insertItems (db, &quot;Corriandor&quot;, &quot;a bag of corriandor&quot;, R.drawable.corriandor);
  }
  
  public void insertItems (SQLiteDatabase db, String name, String description, int image) {
      ContentValues contentValues = new ContentValues();
      contentValues.put (&quot;NAME&quot;, name);
      contentValues.put (&quot;DESCRIPTION&quot;, description);
      contentValues.put (&quot;IMAGE_RESOURCE_ID&quot;, image);
      db.insert (&quot;ITEMS&quot;, null, contentValues);
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
  }
  }
  </code></pre>
  <p>Here is the exception I receive when I run the code:</p>
  <pre><code>2020-07-30 11:00:09.754 28815-28815/com.myapps.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.myapps.myapplication, PID: 28815
  android.content.res.Resources$NotFoundException: Resource ID #0x0
      at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:246)
      at android.content.res.Resources.getDrawableForDensity(Resources.java:905)
      at android.content.res.Resources.getDrawable(Resources.java:845)
      at android.content.Context.getDrawable(Context.java:687)
      at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:455)
      at com.myapps.myapplication.captionedImagesAdapter.onBindViewHolder(captionedImagesAdapter.java:44)
      at com.myapps.myapplication.captionedImagesAdapter.onBindViewHolder(captionedImagesAdapter.java:14)
      at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7178)
      at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7258)
      at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6125)
      at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6391)
      at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6231)
      at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6227)
      at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
      at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:572)
      at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
      at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
      at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
      at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4230)
      at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3941)
      at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4499)
      at android.view.View.layout(View.java:22085)
      at android.view.ViewGroup.layout(ViewGroup.java:6290)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
      at android.view.View.layout(View.java:22085)
      at android.view.ViewGroup.layout(ViewGroup.java:6290)
      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
      at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
      at android.view.View.layout(View.java:22085)
      at android.view.ViewGroup.layout(ViewGroup.java:6290)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
      at android.view.View.layout(View.java:22085)
      at android.view.ViewGroup.layout(ViewGroup.java:6290)
      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
      at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
      at android.view.View.layout(View.java:22085)
      at android.view.ViewGroup.layout(ViewGroup.java:6290)
      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
      at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
      at com.android.internal.policy.DecorView.onLayout(DecorView.java:786)
      at android.view.View.layout(View.java:22085)
      at android.view.ViewGroup.layout(ViewGroup.java:6290)
      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3333)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2810)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1930)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7988)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1154)
      at android.view.Choreographer.doCallbacks(Choreographer.java:977)
      at android.view.Choreographer.doFrame(Choreographer.java:893)
      2020-07-30 11:00:09.754 28815-28815/? E/AndroidRuntime:     at 
      android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:1082)
      at android.os.Handler.dispatchMessage(Handler.java:107)
      at android.os.Looper.loop(Looper.java:214)
      at android.app.ActivityThread.main(ActivityThread.java:7682)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
  </code></pre>