title: OnClickListener NullPointerExpection Java
body: <p>I have build a RecyclerViewer application in Java using android studio.
  When I run the app it crashes, when I checked the Logcat I found there is a null pointer exception in my Adapter.
  <code>onBindViewHolder</code> method.
  This covers line 49.</p>
  <pre><code>@Override
      public void onBindViewHolder(@NonNull ContactsAdapter.ViewHolder holder, int position) {
          holder.txtName.setText(contacts.get(position).getName());
          holder.txtName.setText(contacts.get(position).getEmail());
          Glide.with(context).asBitmap().load(contacts.get(position).getImage()).into(holder.image);
  
          holder.parent.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Toast.makeText(context, contacts.get(position).getName() + &quot; Clicked&quot;, Toast.LENGTH_SHORT).show();
              }
          });
  
  
      }
  </code></pre>
  <p>Logcat log</p>
  <pre><code>    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
          at com.example.learningapplication.ContactsAdapter.onBindViewHolder(ContactsAdapter.java:49)
          at com.example.learningapplication.ContactsAdapter.onBindViewHolder(ContactsAdapter.java:19)
  </code></pre>
  <p>EDIT:
  Here is the full code:</p>
  <pre><code>package com.example.learningapplication;
  
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.annotation.NonNull;
  import androidx.cardview.widget.CardView;
  import androidx.recyclerview.widget.RecyclerView;
  
  import com.bumptech.glide.Glide;
  
  import java.util.ArrayList;
  
  public class ContactsAdapter extends RecyclerView.Adapter&lt;ContactsAdapter.ViewHolder&gt;{
      private Context context;
  
      ArrayList&lt;Contacts&gt; contacts = new ArrayList&lt;&gt;();
  
      public void setContacts(ArrayList&lt;Contacts&gt; contacts) {
          this.contacts = contacts;
          notifyDataSetChanged();
      }
  
      public ContactsAdapter(Context context) {
          this.context = context;
      }
  
      public ContactsAdapter() {
      }
  
      @NonNull
      @Override
      public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_layout, parent, false);
          return new ViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull ContactsAdapter.ViewHolder holder, int position) {
          holder.txtName.setText(contacts.get(position).getName());
          holder.txtName.setText(contacts.get(position).getEmail());
          Glide.with(context).asBitmap().load(contacts.get(position).getImage()).into(holder.image);
  
          holder.parent.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Toast.makeText(context, contacts.get(position).getName() + &quot; Clicked&quot;, Toast.LENGTH_SHORT).show();
              }
          });
  
  
      }
  
      @Override
      public int getItemCount() {
          return contacts.size();
      }
  
      public static class ViewHolder extends RecyclerView.ViewHolder{
          private TextView txtName;
          private TextView txtEmail;
          private ImageView image;
          private CardView parent;
  
          public ViewHolder(@NonNull View itemView) {
              super(itemView);
              txtName = itemView.findViewById(R.id.txtName);
              txtEmail = itemView.findViewById(R.id.txtEmail);
              image = itemView.findViewById(R.id.image);
  
          }
  
      }
  }
  
  ``
  </code></pre>