title: android - Application crashes upon modifying ArrayList<ObjectType>
body: <p>I'm working on an application which will have a <code>ListView</code> alongwith a <code>Button</code> to add items to the <code>ListView</code> dynamically. I've made a custom adapter for the <code>ListView</code>. My idea is when the user will tap the <code>Button</code> it'll show an <code>AlertDialog</code> with a <code>TextView</code> and two buttons, <strong>"OK"</strong> and <strong>"Cancel"</strong>. This my activity where the <code>ListView</code> will be shown - </p>
  
  <pre><code>import java.util.ArrayList;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.app.AlertDialog.Builder;
  import android.app.Dialog;
  import android.content.DialogInterface;
  import android.graphics.Typeface;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.View;
  import android.view.animation.Animation;
  import android.view.animation.AnimationUtils;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class Add_Topics extends Activity {
  
  AddTopics_MenuAdapter adapter;
  ListView topics_list;
  public ArrayList&lt;topic&gt; the_subjects;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_add__topics);
  
      the_subjects = Model.loadTopics();
  
      topics_list = (ListView) findViewById(R.id.topics_list);
  
      overridePendingTransition(R.anim.reverse_in_left, R.anim.reverse_out_left);
  
      Animation enter = AnimationUtils.loadAnimation(getBaseContext(), R.anim.upcoming_menu);
      Animation enter_slow = AnimationUtils.loadAnimation(getBaseContext(), R.anim.enter_l2r_slide);
  
      TextView des = (TextView)findViewById(R.id.des_at);
      TextView title = (TextView)findViewById(R.id.title_at);
  
      Button add_topic = (Button)findViewById(R.id.add_topic_button);
  
      Typeface roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
  
      des.setTypeface(roboto_lt);
      title.setTypeface(roboto_lt);
      add_topic.setTypeface(roboto_lt);
  
      title.startAnimation(enter);
      des.startAnimation(enter_slow);
  
      adapter = new AddTopics_MenuAdapter(this, the_subjects);
      topics_list.setAdapter(adapter);
  }
  
  public void onClickAddTopic(View v) {
      showDialog(0);
  }
  protected Dialog onCreateDialog(int id) {
      switch(id) {
      case 0: 
          LayoutInflater li = LayoutInflater.from(this);
          View edt_txt_add_tpc = li.inflate(R.layout.add_topics_res, null);
          edt_txt_add_tpc.requestFocus();
          final EditText tpc_nm = (EditText) edt_txt_add_tpc.findViewById(R.id.topic_name);
          Builder bld = new AlertDialog.Builder(this);
          bld.setIcon(R.drawable.ic_launcher);
          bld.setTitle("Add Topic/Subject");
          bld.setView(edt_txt_add_tpc);
          bld.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  
              public void onClick(DialogInterface dialog, int whichButon) {
                  String new_topic_name = tpc_nm.getText().toString();
                  adapter = null;
                  the_subjects.add(new topic(new_topic_name));
                  adapter = new AddTopics_MenuAdapter(getBaseContext(), the_subjects);
                  adapter.notifyDataSetChanged();
              }
  
          });
          bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  
              public void onClick(DialogInterface dialog, int whichButton) {
  
          }
          });
          return bld.create();
      }
      return null;
  } 
  
  @Override
  public void onBackPressed() {
      super.onBackPressed();
      overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_add__topics, menu);
      return true;
      }
  }
  </code></pre>
  
  <p>But, the whole application crashes when I tap the <strong>"OK"</strong> button. It works fine if I omit <code>the_subjects.add(new topic(new_topic_name));</code>. But then the newly added item disappears when I reload the activity.</p>
  
  <p>Here is my custom <code>ListView</code> adapter - </p>
  
  <pre><code>import java.util.ArrayList;
  
  import android.content.Context;
  import android.graphics.Typeface;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.animation.Animation;
  import android.view.animation.AnimationUtils;
  import android.widget.ArrayAdapter;
  import android.widget.TextView;
  
  public class AddTopics_MenuAdapter extends ArrayAdapter&lt;topic&gt; {
  
  private final Context context;
  final ArrayList&lt;topic&gt; topics;
  
  public AddTopics_MenuAdapter(Context context, ArrayList&lt;topic&gt; topics) {
      super(context, R.layout.add_topics_menu, topics);
      this.context = context;
      this.topics = topics;
  }
  @Override 
  public View getView(int position, View convertView, ViewGroup parent) {
  
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      View topicsView = inflater.inflate(R.layout.add_topics_menu, parent, false);
  
      TextView topic_name = (TextView) topicsView.findViewById(R.id.topic_title);
  
      Typeface rbt_lt = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
  
      Animation enter = AnimationUtils.loadAnimation(getContext(), R.anim.upcoming_menu);
  
      topic_name.setText(topics.get(position).toString());
      topic_name.setTypeface(rbt_lt);
      topic_name.setAnimation(enter);
  
      return topicsView;
  
      }
  }
  </code></pre>
  
  <p>Model.java - </p>
  
  <pre><code>import java.util.ArrayList;
  
  import android.util.Log;
  import android.widget.Toast;
  
  public class Model {
  
  public static ArrayList&lt;topic&gt; list_of_topics;
  public static String[] subjects;
  
  public static ArrayList&lt;topic&gt; loadTopics() {
      list_of_topics = new ArrayList&lt;topic&gt;();
      list_of_topics.add(new topic("History"));
      list_of_topics.add(new topic("Geography")); 
      return list_of_topics;
  }
  }
  </code></pre>
  
  <p>topic.java - </p>
  
  <p>public class topic {</p>
  
  <pre><code>String Topic;
  
  public topic(String topic) {
      Topic = topic;
  }
  }
  </code></pre>
  
  <p>Here's the log of my error - </p>
  
  <pre><code>03-07 18:13:28.271: E/AndroidRuntime(20349): FATAL EXCEPTION: main
  03-07 18:13:28.271: E/AndroidRuntime(20349): java.lang.NullPointerException
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at com.Swap.StudyBuddy.Add_Topics$1.onClick(Add_Topics.java:80)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:170)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at android.os.Handler.dispatchMessage(Handler.java:107)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at android.os.Looper.loop(Looper.java:194)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at android.app.ActivityThread.main(ActivityThread.java:5409)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at java.lang.reflect.Method.invoke(Method.java:525)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
  03-07 18:13:28.271: E/AndroidRuntime(20349):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I can't figure out figure out how to modify the <code>ArrayList&lt;topic&gt;</code> without crashing the app. Please help me to solve this problem. Thanks in advance!!</p>