Getting null pointer exception while getting tag of card view
<p>I have created a custom list which has a card view, button , text view and a switch. I have set tag to the card view.</p>

<p>But when I tried to retrieve this tag in table view holder it's throwing a null pointer exception.</p>

<p>If I get the tag inside a listener it dose not throw any exception. I don't know from where to access this tag. </p>

<p>I want to do setChecked true for switch if the status is one and setChecked should be false if the status is 0. </p>

<p>But I don't get where to do this?</p>

<p>CustomAlertAdapter </p>

<pre><code>public class TableListAdapter extends RecyclerView.Adapter&lt;TableListAdapter.TableViewHolder&gt; {


    public static TimeTableHelper db;
    public static TimeTableList timeTableList;

    public static int cardId,id,status=0;
    public static boolean editMode;
    public static List&lt;TimeTable&gt; tableList;
    public static TimeTable t = new TimeTable();
    public static TimeTable ci;

    public TableListAdapter(List&lt;TimeTable&gt; tableList,TimeTableList timeTableList) {
        this.tableList = tableList;
        this.timeTableList = timeTableList;
    }
    private Context context;

    public TableListAdapter(Context context) {
        this.context = context;
    }
    @Override
    public int getItemCount() {
          return tableList.size();
    }

    @Override
    public void onBindViewHolder(TableViewHolder tableViewHolder, int i) {


        ci = tableList.get(i);

        tableViewHolder.cv.setTag(i);

        tableViewHolder.aSwitch.setTag(i);

        Log.d("setId", String.valueOf(i));
        tableViewHolder.tableTitle.setText(ci.getTitle());

        ((GradientDrawable)tableViewHolder.color.getBackground()).setColor(ci.getTableColor());



    }

   @Override
   public TableViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View  itemView = LayoutInflater.
                   from(viewGroup.getContext()).
                   inflate(R.layout.table_card, viewGroup, false);


       return new TableViewHolder(itemView);
   }

  public static class TableViewHolder extends RecyclerView.ViewHolder {

      protected TextView tableTitle;
      protected CardView cv;
      protected SwitchCompat aSwitch;
      protected Button color;


      public TableViewHolder(final View v) {
          super(v);
          tableTitle = (TextView) v.findViewById(R.id.tableTitle);
          cv = (CardView) v.findViewById(R.id.card_view);
          aSwitch = (SwitchCompat) v.findViewById(R.id.switch2);
          color = (Button) v.findViewById(R.id.selectColor);

          db = new TimeTableHelper(timeTableList);


          cv.setOnLongClickListener(new View.OnLongClickListener() {

              @Override
              public boolean onLongClick(final View v) {
                  // TODO Auto-generated method stub

                  final AlertDialog.Builder builder = new AlertDialog.Builder(timeTableList);

                  builder.setTitle("Delete Time Table")
                          .setMessage("Are you sure you want to Delete this Time Table?")
                          .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {

                                  cardId = (int)v.getTag();

                                  Log.d("cardId", String.valueOf(cardId));

                                  t = tableList.get(cardId);
                                  id = t.getId();

                                  t = db.getTable(id);
                                  db.deleteTable(t);

                                  Intent intent = new Intent(timeTableList,TimeTableList.class);
                                  timeTableList.finish();
                                  timeTableList.startActivity(intent);

                              }

                          })


                          .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
                                  // do nothing
                              }
                          })

                          .setIcon(R.drawable.ic_warning_black_36dp)
                          .show();
                  return true;
              }

          });


          cv.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(final View v) {

                  cardId = (int)v.getTag();

                  Log.d("cardId", String.valueOf(cardId));

                  t = tableList.get(cardId);
                  id = t.getId();

                  Log.d("Id",String.valueOf(id));

                  editMode = true;
                  Intent i = new Intent(timeTableList, NewTimeTable.class);
                  i.putExtra("editMode", editMode);
                  i.putExtra("tableId", id);
                  timeTableList.startActivity(i);
              }
          });


          cardId = (int) cv.getTag();

          Log.d("cardId", String.valueOf(cardId));

          t = tableList.get(cardId);
          id = t.getId();

          if(status == 1)
          {
              aSwitch.setChecked(true);
          }
          else
          {
              aSwitch.setChecked(false);
          }


          aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {



                  if (isChecked) {

                      status = 1;

                      t = db.getTable(id);
                      t.setStatus(status);
                      db.updateStatus(t);

                      Log.d("status", String.valueOf(status));

                      final List&lt;TimeTable&gt; events = db.getAllTables();
                      for (TimeTable cn : events) {
                          String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
                                  "Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
                          Log.d("Data ", log);
                      }

                  } else {

                      status = 0;

                      t = db.getTable(id);
                      t.setStatus(status);
                      db.updateStatus(t);
                      final List&lt;TimeTable&gt; events = db.getAllTables();
                      for (TimeTable cn : events) {
                          String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
                                  "Status: " + cn.getStatus() + ",color: " + cn.getTableColor();
                          Log.d("Data ", log);
                      }
                      Log.d("status", String.valueOf(status));
                  }

              }
          });

      }
  }

    public void updateAdapaterList(List&lt;TimeTable&gt; newTimeTableList) {
        //Replace the current list with new list
        this.tableList = newTimeTableList;
        //notify the adapter that the data set has changed
        notifyDataSetChanged();
    }
}
</code></pre>

<p>Thank you.</p>