title: ArrayList<String>() and SharedPreference using ObjectSerializer
body: <p>I have an application that saves an array list of <code>String</code> and serializes it using an <code>ObjectSerializer</code> then saves that serialized list in a <code>SharedPreferences</code> instance. </p>
  
  <p>It involves two activities "putting" and "getting" from the same <code>SharePreferences</code> object.</p>
  
  <p>In a <code>ListActivity</code>, I long-click an item to start a deletion process. After the <code>String</code> deletes, I re-initialize the <code>ListAdapter</code> with the updated <code>ArrayList&lt;String&gt;</code> object to update the <code>ListView</code>. After I restart the <code>ListAcivity</code> by calling it again, the item REAPPEARS in my list. It causes a <code>NullPointerException</code> when clicked because it now points to an object that doesnt exist anymore (<code>null</code>).</p>
  
  <p>I have tried removing items from the <code>ArrayList&lt;String&gt;</code> and then putting it in the <code>ObjectSerializer</code> then <code>SharedPreferences</code>. I even tried removing the old <code>SharedPreference</code> value (using <code>clear()</code> or <code>remove()</code> methods). Maybe I'm just overlooking a step, but I cannot figure out what can be causing it.</p>
  
  <p>Would anyone be able to show or walk me through how to serialize an <code>ArrayList&lt;String&gt;</code>, save to the <code>SharedPreference</code> object, remove and item from the list, and update the <code>SharedPreference</code> object so I can get the same (correctly updated list) <code>SharedPreference</code> object from another activity?</p>
  
  <p>Thank you so much!</p>
  
  <p>Below is my (shortened) code:</p>
  
  <p>Activity 1:</p>
  
  <pre><code>public static final String TITLE_PREF = "titleList";
  ArrayList&lt;String&gt; titleList = new ArrayList&lt;&gt;();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      saveBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              saveSongFile(title, song);
          }
      });
  }
  
  private void saveSongFile(String title, String song) {
      BufferedWriter bufferWriter = null;
      try {
          FileOutputStream fos = openFileOutput(title, Context.MODE_PRIVATE);
          bufferWriter = new BufferedWriter(new OutputStreamWriter(fos));
          bufferWriter.write(song);
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              bufferWriter.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      // new songs (not updated songs) go to the top
      if (!titleList.contains(title))
          titleList.add(0, title);
      mSetTitleListAdapter(titleList);
      saveTitleArray();
  }
  
  private void saveTitleArray() {
      prefEditor = titlePref.edit();
      try {
          prefEditor.putString(TITLE_PREF, ObjectSerializer.serialize(titleList));
      } catch (IOException e) {
          e.printStackTrace();
      }
      prefEditor.apply();
  }
  
  private void mSetTitleListAdapter(ArrayList&lt;String&gt; List) {
      autoCompleteAdapter = new ArrayAdapter&lt;&gt;(
              this,
              android.R.layout.simple_list_item_1,
              List
      );
      titleBox.setAdapter(autoCompleteAdapter);
  }
  </code></pre>
  
  <p>Activity 2 (ListActivity):</p>
  
  <pre><code>public static final String EXTRA_NAME = "com.frankbrenyah.lyricmaker.NAME";
  public static final String TITLE_PREF = "titleList";    
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_pick_song_list);
  
      //init string array from blank list or sharedPref saved data
      songListArray = new ArrayList&lt;&gt;();
      titlePref = getSharedPreferences(TITLE_PREF, MODE_PRIVATE);
      try {
          songListArray = (ArrayList&lt;String&gt;) ObjectSerializer.deserialize(
                  titlePref.getString(TITLE_PREF,
                          ObjectSerializer.serialize(new ArrayList&lt;String&gt;())
                  )
          );
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
      mSetListAdapter(songListArray);
  
      getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view, final int position, long id) {
              final AlertDialog.Builder confirmDel = new AlertDialog.Builder(pickSong_List.this);
              confirmDel.setTitle("Delete?")
                      .setMessage("This cannot be undone!")
                      .setPositiveButton("DO NOT delete", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              //system default is dismiss()
                          }
                      })
                      .setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              //delete song, then update title list
                              getApplicationContext().deleteFile(songListArray.get(position));
                              songListArray.remove(position);
                              updateSharedPref(titlePref, TITLE_PREF, songListArray);
                              mSetListAdapter(songListArray);
                              Toast.makeText(
                                      getApplicationContext(),
                                      "Deleted",
                                      Toast.LENGTH_SHORT).show();
                          }
                      });
              confirmDel.create().show();
              return true;
          }
      });
  }
  
  @Override
  protected void onListItemClick (ListView l, View v, int position, long id){
      //return selected song to main activity so it can be loaded into text box's
      Intent resultIntent = new Intent(EXTRA_NAME, Uri.parse(songListArray.get(position)));
      setResult(RESULT_OK, resultIntent);
      finish();
  }
  
  private void mSetListAdapter(ArrayList&lt;String&gt; list) {
      final ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;&gt;(
              this,
              android.R.layout.simple_list_item_1,
              list
      );
      setListAdapter(arrayAdapter);
  }
  
  private void updateSharedPref(SharedPreferences sharedPref,
                                String prefFileName,
                                ArrayList&lt;String&gt; list) {
  
      SharedPreferences.Editor editor = sharedPref.edit();
      try {
          editor.putString(prefFileName, ObjectSerializer.serialize(list));
      } catch (IOException e) {
          e.printStackTrace();
      }
      editor.apply();
  }
  </code></pre>
  
  <p><strong>ADDITION</strong>: Below is one error instance from a randomly selected user.</p>
  
  <pre><code>java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,
    request=0, result=-1, data=Intent { act=com.my.app.NAME
    dat=Scrivo una Canzone }} to activity
    {com.my.app/com.my.app.ActivityONE}:
    java.lang.NullPointerException: Attempt to invoke virtual method 'void
    java.io.BufferedReader.close()' on a null object reference
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3633)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3676)
    at android.app.ActivityThread.access$1300(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5351)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    ... 10 more
  </code></pre>