0

I am trying to remove a specific item when I click on it. I have run logs so I know that it's picking an item I just don't understand what the error means. If it is important I am using room database and using a recycler view.

java.lang.NullPointerException: Attempt to invoke virtual method 'void uk.edu.le.co2103.myrecipebook.RecipeListAdapter.notifyItemRemoved(int)' on a null object reference at uk.edu.le.co2103.myrecipebook.MainActivity$4.onClick(MainActivity.java:126)

I have checked that there is data in the list hence the logs below.

D/MAIN ACTIVITY: List: [uk.edu.le.co2103.myrecipebook.Recipe@c97e34d, uk.edu.le.co2103.myrecipebook.Recipe@e7b6e02, uk.edu.le.co2103.myrecipebook.Recipe@d9ea313] 2020-04-01 23:07:29.300 25679-25679/uk.edu.le.co2103.myrecipebook D/MAIN ACTIVITY: Index: 2

Here is the code for my deletion part of main Activity

    public void onItemClick(int position, View view) {
        Log.d(TAG, "onItemClick Position: " + position);
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Edit or Delete...");
    alertDialog.setPositiveButton("Edit", new 
 DialogInterface.OnClickListener()
 alertDialog.setNegativeButton("Delete", new 
 DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //Delete
            Log.d(TAG, "List: " + recipesList);
            Log.d(TAG, "Index: " + position);
            int removeIndex = position;
            recipesList.remove(removeIndex);
            mAdapter.notifyItemRemoved(removeIndex);


        }
    });

My Adapter Code

 public class RecipeListAdapter extends 
 RecyclerView.Adapter<RecipeListAdapter.RecipeViewHolder> {
private OnItemClickListener mListener;
private LiveData<List<Recipe>> recipeList;


public interface OnItemClickListener{
    void onItemClick(int position, View view);

}
public void setOnItemClickListener(OnItemClickListener listener){
    mListener = listener;
}




class RecipeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private final TextView recipeItemView;

    private RecipeViewHolder(View itemView) {
        super(itemView);
        recipeItemView = itemView.findViewById(R.id.textView);
        itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (mListener != null){
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION){
                        mListener.onItemClick(position, v);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        mListener.onItemClick(getAdapterPosition(), view);
    }
}

private final LayoutInflater mInflater;
private List<Recipe> mRecipes; // Cached copy of words

RecipeListAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
    this.recipeList = recipeList;
}

@Override
public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = mInflater.inflate(R.layout.recyclerview_item, parent, false);
    return new RecipeViewHolder(itemView);
}

@Override
public void onBindViewHolder(RecipeViewHolder holder, int position) {
    if (mRecipes != null) {
        Recipe current = mRecipes.get(position);
        holder.recipeItemView.setText(current.getName());
    } else {
        // Covers the case of data not being ready yet.
        holder.recipeItemView.setText("No Recipes");
    }
}

void setWords(List<Recipe> recipes){
    mRecipes = recipes;
    notifyDataSetChanged();
}


// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount() {
    if (mRecipes != null)
        return mRecipes.size();
    else return 0;
}
public interface OnNoteListener{}

}

  • 1
    That would mean that `mAdapter` is null. – Mike M. Apr 01 '20 at 22:15
  • is there anyway I could do it without madapter? – Fred Fredricks Apr 01 '20 at 22:17
  • No, not really. You just need to make sure that `mAdapter` points to the `Adapter` instance that you set on the `RecyclerView`. – Mike M. Apr 01 '20 at 22:18
  • It's nothing to do with that. You simply need to assign `mAdapter` correctly. Somewhere you're setting an `Adapter` on the `RecyclerView`; e.g., `RecipeListAdapter adapter = new RecipeListAdapter(...);`, `recyclerView.setAdapter(adapter);`. You need to change that to assign and use `mAdapter` instead; e.g., `mAdapter = new RecipeListAdapter(...);`, `recyclerView.setAdapter(mAdapter);`. – Mike M. Apr 01 '20 at 22:30
  • That's stupid me. I have changed all references of 'adapter' to 'mAdapter' but I am still getting the same error? – Fred Fredricks Apr 01 '20 at 22:56
  • Well, if it's the exact same Exception and message and location, then `mAdapter` is still null there, somehow, but we'd have to see exactly where and how you're declaring it, and where and how you're assigning it. If the Exception or message or location is different, then you've got another issue, too. – Mike M. Apr 01 '20 at 23:21

2 Answers2

0

Can you share your adapter code here? I had this problem in the past and it happens because you need to refresh your list contents inside your adapter (you are now only noticing it with .notifyItemRemoved(int index) ) but I realy need to see your adapter to help you about how to do it, in my situation an observer was the best way.

thiago
  • 103
  • 5
0

I think you didn't initialize the adapter or the list.

Ritu Suman Mohanty
  • 734
  • 1
  • 6
  • 15