title: Adapter's onBindView gives error: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
body: <p>for some reason when I try to run my code, it pops up with <strong>java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                   at java.util.ArrayList.get(ArrayList.java:411)
                                                                   at krispo.callie.ContactAdapter.onBindViewHolder(ContactAdapter.java:96)
                                                                   at krispo.callie.ContactAdapter.onBindViewHolder(ContactAdapter.java:27)</strong></p>
  
  <p>Could someone explain what any of this means and how I could solve the problem? I've searched for answers but none have worked. </p>
  
  <pre><code>package krispo.callie;
  
  import android.content.Context;
  import android.support.v7.widget.RecyclerView;
  import android.util.Log;
  import android.util.SparseBooleanArray;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.CheckBox;
  import android.widget.CheckedTextView;
  import android.widget.CompoundButton;
  import android.widget.ImageView;
  import android.widget.RadioGroup;
  import android.widget.TextView;
  import android.widget.RadioGroup.OnCheckedChangeListener;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  /**
   * Created by 21poonkw1 on 9/8/2018.
   */
  
  public class ContactAdapter extends RecyclerView.Adapter&lt;ContactAdapter.MyViewHolder&gt; {
  
      private List&lt;Contact&gt; contactsList;
      private static List&lt;Contact&gt; mSelectedItemsIds;
      private int rowLayout;
  
  
  
  
      public class MyViewHolder extends RecyclerView.ViewHolder {
          public TextView name, number;
          public CheckBox checkBox;
          public MyViewHolder(View view) {
              super(view);
  
              if(rowLayout == 1) {
                  name = (TextView) view.findViewById(R.id.name);
                  number = (TextView) view.findViewById(R.id.number);
                  checkBox = (CheckBox) view.findViewById(R.id.checkBox);
              }
  
              mSelectedItemsIds = new ArrayList&lt;&gt;();
              contactsList = new ArrayList&lt;&gt;();
  
  
  
          }
      }
  
  
      public ContactAdapter(List&lt;Contact&gt; contactsList,int rowLayout) {
          this.contactsList = contactsList;
          this.rowLayout = rowLayout;
      }
  
  
      @Override
      public int getItemCount(){
  
          return contactsList.size();
      }
  
  
      @Override
      public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  
          View itemView;
  
          if(rowLayout == 1) {
              itemView = LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.contact_row, parent, false);
          }
          else{
              itemView = LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.contact_card, parent, false);
          }
  
          return new MyViewHolder(itemView);
      }
  
      public List&lt;Contact&gt; getSelectedIds() {
  
          return mSelectedItemsIds;
      }
  
      @Override
  
      public void onBindViewHolder(final MyViewHolder holder, final int position) {
  
          final Contact contact = contactsList.get(position);
          holder.name.setText(contact.getName());
          holder.number.setText(contact.getPhoneNumber());
  
          holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
                  Contact currentContact = contactsList.get(position);
                  if (isChecked) {
                      mSelectedItemsIds.add(currentContact);
                  } else {
                      mSelectedItemsIds.remove(mSelectedItemsIds.indexOf(currentContact));
                      notifyItemRangeChanged(position, getItemCount());
                  }
              }
          });
  
      }
  
  
  
  
  
  
  
  }
  </code></pre>