This question has already been answered:

public class ScrollingActivitySalads extends AppCompatActivity { ... public class RVAdapterSalads extends RecyclerView.Adapter<RVAdapterSalads.CardViewHolder> { public class CardViewHolder extends RecyclerView.ViewHolder { TextView dishName = (TextView) itemView.findViewById(R.id.dish_name); TextView dishConsist = (TextView) itemView.findViewById(R.id.dish_consist); ImageView dishPhoto = (ImageView) itemView.findViewById(R.id.dish_photo); int currentCardPosition; Context mContext; String allDishes = ""; CardView cardView = (CardView) itemView.findViewById(R.id.cv); CardViewHolder(View itemView, Context context) { super(itemView); mContext = context; cardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mContext instanceof ScrollingActivitySalads){ ((ScrollingActivityHots)mContext).showSnackbar(currentCardPosition+1); allDishes += "dishName"; allDishes += "/n"; } } }); intent.putExtra("allSalads", allDishes);//error } }.... 

In the marked place, an error NullPointerException. I noticed that the background of the variables allDishes, if clicked, is red for everyone, and the variable in the marked line is green. Perhaps another variable is perceived inside putExtra? How to fix?

Reported as a duplicate by participants Enikeyschik , 0xdb , Community Spirit May 10 at 15:33 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Variables of complex types, classes, such as String, or Intent, for which we did not ask for memory using the words new Intent(); , or new (класс, который является наследником Intent); , by default point to null, this is a special data type, so a NullPointerException pops up. Write an Intent intent = new Intent(); and you can easily call the methods of this class.