title: How to implement "favourite" button feature (like favourite recipe/food) and display on another list in another fragment
body: <p>I want to have a feature that when the user clicked the button on a certain row, it will add the row to another list which is called favorite list. Currently i have created database that also include favourite status. I already tried to start with creating a button that when its clicked it will change the fav status.
  Im still new on android studio, just learnt for half a month. So go easy on me. I am currently stuck on this error:</p>
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.myapplication, PID: 13778
  java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.myapplication.data.DatabaseHandler.addRecipe(com.example.myapplication.model.Recipe)' on a null object reference
      at com.example.myapplication.adapter.RecyclerViewAdapter$ViewHolder$1.onClick(RecyclerViewAdapter.java:123)
      at android.view.View.performClick(View.java:7448)
      at android.view.View.performClickInternal(View.java:7425)
      at android.view.View.access$3600(View.java:810)
      at android.view.View$PerformClick.run(View.java:28305)
      at android.os.Handler.handleCallback(Handler.java:938)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:223)
      at android.app.ActivityThread.main(ActivityThread.java:7656)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
  </code></pre>
  <p>Recipe.java</p>
  <pre><code>public class Recipe {
      private int id;
      private String name;
      private String description;
      private String ingredient;
      private int image;
      private String favStatus;
      
      
      public Recipe() {
      }
      
      public Recipe(int id, String name, String description, String ingredient, int image, String favStatus) {
          this.id = id;
          this.name = name;
          this.description = description;
          this.ingredient = ingredient;
          this.image = image;
          this.favStatus = favStatus;
      }
      
      
      public int getId() {
          return id;
      }
      
      public void setId(int id) {
          this.id = id;
      }
      
      public String getName() {
          return name;
      }
      
      public void setName(String name) {
          this.name = name;
      }
      
      public String getDescription() {
          return description;
      }
      
      public void setDescription(String description) {
          this.description = description;
      }
      
      public String getIngredient() {
          return ingredient;
      }
      
      public void setIngredient(String ingredient) {
          this.ingredient = ingredient;
      }
      
      public int getImage() {
          return image;
      }
      
      public void setImage(int image) {
          this.image = image;
      }
      
      public String getFavStatus() {
          return favStatus;
      }
      
      public void setFavStatus(String favStatus) {
          this.favStatus = favStatus;
      }
  }
  </code></pre>
  <p>DatabaseHandler.java</p>
  <pre><code>public class DatabaseHandler extends SQLiteOpenHelper {
  
      public DatabaseHandler(Context context) {
          super(context, Util.DATABASE_NAME, null, Util.DATABASE_VERSION);
      }
  
  
      //We create our table..
      @Override
      public void onCreate(SQLiteDatabase db) {
          //SQL- Structured Query Language
         /* 
          create table _name(id, name, desc, ingredient, image);
          */
          String CREATE_CONTACT_TABLE = &quot;CREATE TABLE &quot; + Util.TABLE_NAME + &quot;(&quot;
                  + Util.KEY_ID + &quot; INTEGER PRIMARY KEY,&quot; + Util.KEY_NAME + &quot; TEXT,&quot;
                  + Util.KEY_DESCRIPTION + &quot; TEXT,&quot; + Util.KEY_INGREDIENT + &quot; TEXT,&quot;
                  + Util.KEY_IMAGE + &quot; BLOB,&quot; + Util.KEY_FAV_STATUS + &quot; TEXT&quot; + &quot;)&quot;;
          db.execSQL(CREATE_CONTACT_TABLE); //Creating our table..
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          String DROP_TABLE = String.valueOf(R.string.db_drop);
          db.execSQL(DROP_TABLE, new String[]{Util.DATABASE_NAME});
  
          //Create table again
          onCreate(db);
      }
  
      //Add Recipe
      public void addRecipe(Recipe recipe) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(Util.KEY_NAME, recipe.getName());
          values.put(Util.KEY_DESCRIPTION, recipe.getDescription());
          values.put(Util.KEY_INGREDIENT, recipe.getIngredient());
          values.put(Util.KEY_IMAGE, recipe.getImage());
          values.put(Util.KEY_FAV_STATUS, recipe.getFavStatus());
  
          //Insert into row..
          db.insert(Util.TABLE_NAME, null, values);
          Log.d(&quot;DBHandler&quot;, &quot;addRecipe: &quot; + &quot;item added&quot;);
          db.close();
      }
  
  
      //Get a recipe
      public Recipe getRecipe(int id) {
          SQLiteDatabase db = this.getReadableDatabase();
  
          Cursor cursor = db.query(Util.TABLE_NAME,
                  new String[] { Util.KEY_ID, Util.KEY_NAME, Util.KEY_DESCRIPTION, Util.KEY_FAV_STATUS,
                          Util.KEY_INGREDIENT, Util.KEY_IMAGE}, Util.KEY_ID +&quot;=?&quot;,
                  new String[]{String.valueOf(id)},
                  null, null, null);
  
          if (cursor != null)
              cursor.moveToFirst();
  
          Recipe recipe = new Recipe();
          recipe.setId(Integer.parseInt(cursor.getString(0)));
          recipe.setName(cursor.getString(1));
          recipe.setDescription(cursor.getString(2));
          recipe.setIngredient(cursor.getString(3));
          recipe.setImage(Integer.parseInt(cursor.getString(4)));
          recipe.setFavStatus(cursor.getString(5));
  
          return recipe;
      }
  
  
      //Get all Recipes
      public List&lt;Recipe&gt; getAllRecipes() {
          List&lt;Recipe&gt; recipeList = new ArrayList&lt;&gt;();
  
          SQLiteDatabase db = this.getReadableDatabase();
  
          //Select all recipes
          String selectAll = &quot;SELECT * FROM &quot; + Util.TABLE_NAME;
          Cursor cursor = db.rawQuery(selectAll, null);
  
          //Loop through our data
          if (cursor.moveToFirst()) {
              do {
                  Recipe recipe = new Recipe();
                  recipe.setId(Integer.parseInt(cursor.getString(0)));
                  recipe.setName(cursor.getString(1));
                  recipe.setDescription(cursor.getString(2));
                  recipe.setIngredient(cursor.getString(3));
                  recipe.setImage(Integer.parseInt(cursor.getString(4)));
                  recipe.setFavStatus((cursor.getString(5)));
  
                  //add recipe objects to our list
                  recipeList.add(recipe);
              }while (cursor.moveToNext());
          }
  
          return recipeList;
      }
  
  
      //Update recipe
      public int updateRecipe (Recipe recipe) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(Util.KEY_NAME, recipe.getName());
          values.put(Util.KEY_DESCRIPTION, recipe.getDescription());
          values.put(Util.KEY_INGREDIENT, recipe.getIngredient());
          values.put(Util.KEY_IMAGE, recipe.getImage());
          values.put(Util.KEY_FAV_STATUS, recipe.getFavStatus());
  
          //Update the row
          return db.update(Util.TABLE_NAME, values, Util.KEY_ID + &quot;=?&quot;,
                  new String[]{String.valueOf(recipe.getId())});
      }
  
  
      //Delete single recipe
      public void deleteRecipe(Recipe recipe) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          db.delete(Util.TABLE_NAME, Util.KEY_ID + &quot;=?&quot;,
                  new String[]{String.valueOf(recipe.getId())});
          db.close();
      }
  
      //Select all favorite list method.
      public List&lt;Recipe&gt; getAllFavRecipes() {
          List&lt;Recipe&gt; recipeList = new ArrayList&lt;&gt;();
  
          SQLiteDatabase db = this.getReadableDatabase();
  
          //Select all recipes
          String selectAll = &quot;SELECT * FROM &quot; + Util.TABLE_NAME + &quot; WHERE &quot; + Util.KEY_FAV_STATUS + &quot; ='1'&quot;;
          Cursor cursor = db.rawQuery(selectAll, null);
  
          //Loop through our data
          if (cursor.moveToFirst()) {
              do {
                  Recipe recipe = new Recipe();
                  recipe.setId(Integer.parseInt(cursor.getString(0)));
                  recipe.setName(cursor.getString(1));
                  recipe.setDescription(cursor.getString(2));
                  recipe.setIngredient(cursor.getString(3));
                  recipe.setImage(Integer.parseInt(cursor.getString(4)));
                  recipe.setFavStatus((cursor.getString(5)));
  
                  //add recipe objects to our list
                  recipeList.add(recipe);
              }while (cursor.moveToNext());
          }
  
          return recipeList;
      }
  }
  </code></pre>
  <p>RecyclerViewAdapter.java</p>
  <pre><code>public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.ViewHolder&gt; implements Filterable{
  
      private Context context;
      private List&lt;Recipe&gt; recipeList;
      private List&lt;Recipe&gt; recipeListFull;
      private DatabaseHandler db;
  
  
      public RecyclerViewAdapter(Context context, List&lt;Recipe&gt; recipeList) {
          this.context = context;
          this.recipeList = recipeList;
          recipeListFull = new ArrayList&lt;&gt;(recipeList);
      }
  
      @NonNull
      @Override
      public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
  
          View view = LayoutInflater.from(viewGroup.getContext())
                  .inflate(R.layout.recipe_row, viewGroup, false);
  
          return new ViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
          Recipe recipe = recipeList.get(position); //each recipe object inside of our list
  
          viewHolder.recipeName.setText(recipe.getName());
          viewHolder.description.setText(recipe.getDescription());
          viewHolder.image.setImageResource(recipe.getImage());
      }
  
      @Override
      public int getItemCount() {
          return recipeList.size();
      }
  
      @Override
      public Filter getFilter() {
          return filterRecipe;
      }
  
  
      private Filter filterRecipe = new Filter() {
          @Override
          protected FilterResults performFiltering(CharSequence charSequence) {
              String searchText = charSequence.toString().toLowerCase();
              List&lt;Recipe&gt; tempList = new ArrayList&lt;&gt;();
              if(searchText.length()==0 | searchText.isEmpty()) {
                  tempList.addAll(recipeListFull);
              }else {
                  for (Recipe item:recipeListFull) {
                      if (item.getName().toLowerCase().contains(searchText)) {
                          tempList.add(item);
                      }
                  }
              }
              FilterResults filterResults = new FilterResults();
              filterResults.values = tempList;
  
              return filterResults;
          }
  
          @Override
          protected void publishResults(CharSequence constraint, FilterResults filterResults) {
              recipeList.clear();
              recipeList.addAll((Collection&lt;? extends Recipe&gt;) filterResults.values);
              notifyDataSetChanged();
  
          }
      };
  
  
      public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  
          public TextView recipeName;
          public TextView description;
          public ImageView image;
          public ImageView favBtn;
  
          public ViewHolder(@NonNull  View itemView) {
              super(itemView);
  
              itemView.setOnClickListener(this);
              recipeName = itemView.findViewById(R.id.name);
              description = itemView.findViewById(R.id.description);
              image = itemView.findViewById(R.id.recipe_imageView);
              favBtn = itemView.findViewById(R.id.fav_image_btn);
  
              favBtn.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      int position = getAdapterPosition();
                      Recipe recipe = recipeList.get(position);
  
                      if (recipe.getFavStatus().equals(&quot;0&quot;)) {
                          recipe.setFavStatus(&quot;1&quot;);
                          db.addRecipe(recipe);
                          favBtn.setImageResource(R.drawable.favourite_star);
                      } else {
                          recipe.setFavStatus(&quot;0&quot;);
                          db.deleteRecipe(recipe);
                          favBtn.setImageResource(R.drawable.shadow_fav_star);
                      }
                  }
              });
  
          }
  
          @Override
          public void onClick(View v) {
  
              int position = getAdapterPosition();
              Recipe recipe = recipeList.get(position);
  
              Intent intent = new Intent(context, DetailsActivity.class);
              intent.putExtra(&quot;name&quot;, recipe.getName());
              intent.putExtra(&quot;description&quot;, recipe.getDescription());
              intent.putExtra(&quot;ingredient&quot;, recipe.getIngredient());
              intent.putExtra(&quot;image&quot;, recipe.getImage());
  
              context.startActivity(intent);
  
              //Log.d(&quot;Clicked&quot;, &quot;onClick: &quot; + recipe.getName());
  
          }
      }
  
      //Create method to read and check for fav status for every row..
      
  }
  </code></pre>
  <p>recipeRow.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
      &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
               android:layout_height=&quot;wrap_content&quot;&gt;
  
      &lt;androidx.cardview.widget.CardView
          android:id=&quot;@+id/row_cardView&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;150dp&quot;
          android:layout_marginStart=&quot;1dp&quot;
          android:layout_marginTop=&quot;2dp&quot;
          android:layout_marginEnd=&quot;1dp&quot;
          app:cardCornerRadius=&quot;15dp&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.5&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  
          &lt;androidx.constraintlayout.widget.ConstraintLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              android:background=&quot;@color/white&quot;&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/recipe_imageView&quot;
                  android:layout_width=&quot;200dp&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_marginStart=&quot;3dp&quot;
                  android:scaleType=&quot;centerCrop&quot;
                  app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  app:layout_constraintTop_toTopOf=&quot;parent&quot;
                  tools:srcCompat=&quot;@tools:sample/avatars&quot; /&gt;
  
              &lt;TextView
                  android:id=&quot;@+id/name&quot;
                  android:layout_width=&quot;0dp&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_marginStart=&quot;12dp&quot;
                  android:layout_marginTop=&quot;7dp&quot;
                  android:layout_marginEnd=&quot;12dp&quot;
                  android:ellipsize=&quot;end&quot;
                  android:fontFamily=&quot;@font/courgette&quot;
                  android:maxLines=&quot;2&quot;
                  android:text=&quot;Title Text&quot;
                  android:textColor=&quot;@color/darker&quot;
                  android:textSize=&quot;28sp&quot;
                  android:textStyle=&quot;bold&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                  app:layout_constraintHorizontal_bias=&quot;0.007&quot;
                  app:layout_constraintStart_toEndOf=&quot;@+id/recipe_imageView&quot;
                  app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
              &lt;TextView
                  android:id=&quot;@+id/description&quot;
                  android:layout_width=&quot;0dp&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_marginTop=&quot;10dp&quot;
                  android:ellipsize=&quot;end&quot;
                  android:maxLines=&quot;3&quot;
                  android:text=&quot;Desc Text&quot;
                  android:textSize=&quot;13sp&quot;
                  android:textColor=&quot;@color/darkGray&quot;
                  app:layout_constraintBottom_toTopOf=&quot;@+id/fav_image_btn&quot;
                  app:layout_constraintEnd_toEndOf=&quot;@+id/name&quot;
                  app:layout_constraintStart_toStartOf=&quot;@+id/name&quot;
                  app:layout_constraintTop_toBottomOf=&quot;@+id/name&quot; /&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/fav_image_btn&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_marginEnd=&quot;25dp&quot;
                  android:layout_marginBottom=&quot;7dp&quot;
                  app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                  app:srcCompat=&quot;@drawable/favourite_star&quot; /&gt;
          &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
      &lt;/androidx.cardview.widget.CardView&gt;
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>