title: Listview button click throws null pointer exception
body: <p>Inside my <code>UpdateSupervitionActivity</code> class <code>CallAdapter()</code> method is called to generate the <code>listview</code>. The <code>listview</code> has a button for each <code>item</code> to remove an <code>item</code>. After the <code>listview</code> is loaded, when I try to remove an item from the <code>listview</code> by clicking the listview button, it throws a <code>nullpointer</code> exception and the app crashes.</p>
  
  <p><br><strong>Java class with CallAdapter() method</strong>:<br/></p>
  
  <pre><code>package bd.edu.bubt.regup;
  
  public class UpdateSupervitionActivity extends AppCompatActivity {
  
  EditText fac_code, s_intake, s_section;
  ListView listView;
  Button search, addlist, update;
  Spinner shift;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_update_supervition);
  
      fac_code = (EditText) findViewById(R.id.sh_code);
      s_intake = (EditText) findViewById(R.id.intake);
      s_section = (EditText) findViewById(R.id.section);
      listView = (ListView) findViewById(R.id.faclistview);
      shift = (Spinner) findViewById(R.id.shift);
      addlist = (Button) findViewById(R.id.addlist);
      search = (Button) findViewById(R.id.search);
      update = (Button) findViewById(R.id.update);
  }
  
  public void CallAdapter(Context context)
      {
          adapterlist.clear();
  
          if(daylist.size() == 0)
          {
              //no intake list for day shift
  
              if(evelist.size() == 0)
              {
                  //no intake list for evening shift
              }
              else if(evelist.size() &gt; 0)
              {
                  for (int i=0;i&lt;evelist.size();i++)
                  {
                      String s = evelist.get(i);
                      int p = s.indexOf("-");
  
                      String intake = s.substring(0,p);
                      String section = s.substring(p+1,s.length());
  
                      adapterlist.add(new UpdateSupervisionItem(intake, section, "Evening"));
                  }
              }
          }
          else if(daylist.size() &gt; 0)
          {
              for (int i=0;i&lt;daylist.size();i++)
              {
                  String s = daylist.get(i);
                  int p = s.indexOf("-");
  
                  String intake = s.substring(0,p);
                  String section = s.substring(p+1,s.length());
  
                  adapterlist.add(new UpdateSupervisionItem(intake, section, "Day"));
              }
  
              if(evelist.size() == 0)
              {
                  //no intake list for evening shift
              }
              else if(evelist.size() &gt; 0)
              {
                  for (int i=0;i&lt;evelist.size();i++)
                  {
                      String s = evelist.get(i);
                      int p = s.indexOf("-");
  
                      String intake = s.substring(0,p);
                      String section = s.substring(p+1,s.length());
  
                      adapterlist.add(new UpdateSupervisionItem(intake, section, "Evening"));
                  }
              }
          }
  
          UpdateSupervisionAdapter updateSupervisionAdapter = new UpdateSupervisionAdapter(context, R.layout.update_supervision_list_view_layout, adapterlist);
          listView.setAdapter(updateSupervisionAdapter);
      }
  }
  </code></pre>
  
  <p><br><strong>Adapter class:</strong><br/></p>
  
  <pre><code>package bd.edu.bubt.regup;
  
  public class UpdateSupervisionAdapter extends ArrayAdapter&lt;UpdateSupervisionItem&gt; {
  
      ArrayList&lt;UpdateSupervisionItem&gt; adapterlist = new ArrayList&lt;&gt;();
      Context context;
  
      public UpdateSupervisionAdapter(Context context, int textViewResourceId, ArrayList&lt;UpdateSupervisionItem&gt; objects) {
  
          super(context, textViewResourceId, objects);
          this.context = context;
          adapterlist = objects;
      }
  
      @Override
      public int getCount() {
  
          return super.getCount();
      }
  
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent){
  
          View v = convertView;
          LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = inflater.inflate(R.layout.update_supervision_list_view_layout, null);
  
          TextView intake = (TextView) v.findViewById(R.id.intake);
          TextView section = (TextView) v.findViewById(R.id.section);
          TextView shift = (TextView) v.findViewById(R.id.shift);
          final Button remove = (Button) v.findViewById(R.id.remove);
  
          remove.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.blink_anim);
                  remove.startAnimation(animation);
  
                  UpdateSupervisionItem updateSupervisionItem = getItem(position);
  
                  String in_take = updateSupervisionItem.getIntake();
                  String sec_tion = updateSupervisionItem.getSection();
                  String shi_ft = updateSupervisionItem.getShift();
  
                  UpdateSupervitionActivity updateSupervitionActivity = new UpdateSupervitionActivity();
  
                  if(TextUtils.equals(shi_ft,"Day"))
                  {
                      updateSupervitionActivity.daylist.remove(in_take+ "-" +sec_tion);
                      updateSupervitionActivity.CallAdapter(context);
                  }
                  else if(TextUtils.equals(shi_ft,"Evening"))
                  {
                      updateSupervitionActivity.evelist.remove(in_take+ "-" +sec_tion);
                      updateSupervitionActivity.CallAdapter(context);
                  }
              }
          });
  
          intake.setText("Intake: " +adapterlist.get(position).getIntake());
          section.setText("Section: " +adapterlist.get(position).getSection());
          shift.setText("Shift: " +adapterlist.get(position).getShift());
  
          return v;
      }
  
  }
  </code></pre>
  
  <p><br><strong>Error log:</strong><br/></p>
  
  <pre><code>FATAL EXCEPTION: main
                    Process: bd.edu.bubt.regup, PID: 31219
                    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                        at bd.edu.bubt.regup.UpdateSupervitionActivity.CallAdapter(UpdateSupervitionActivity.java:352)
                        at bd.edu.bubt.regup.UpdateSupervisionAdapter$1.onClick(UpdateSupervisionAdapter.java:70)
                        at android.view.View.performClick(View.java:5637)
                        at android.view.View$PerformClick.run(View.java:22429)
                        at android.os.Handler.handleCallback(Handler.java:751)
                        at android.os.Handler.dispatchMessage(Handler.java:95)
                        at android.os.Looper.loop(Looper.java:154)
                        at android.app.ActivityThread.main(ActivityThread.java:6119)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  </code></pre>