title: How can I save an ArrayList<MyObject> to a file?
body: <p><em>How can I save an ArrayList to a file?
  What am I doing wro</em>ng?</p>
  <p>I have used this SO question to help me with Serializable objects.:</p>
  <p><a href="https://stackoverflow.com/questions/4670215/how-to-serialize-arraylist-on-android">how to serialize ArrayList on android</a></p>
  <p>and I used this SO question on how to write an array list:</p>
  <p><a href="https://stackoverflow.com/questions/16111496/java-how-can-i-write-my-arraylist-to-a-file-and-read-load-that-file-to-the">Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList?</a></p>
  <p>However when I attempt to write the to the file I get the error:</p>
  <blockquote>
  <p>java.io.NotSerializableException: at</p>
  <p>java.io.ObjectOutputStream.writeObject at</p>
  <p>com.mycompany.MyClass.saveData</p>
  </blockquote>
  <p><strong>Here is MyClass that attempts to save the file</strong></p>
  <pre><code>private ArrayList &lt; MyCustomObject &gt; arrayList;
  private File dataFile;
  private String FILE_NAME = &quot;FILE_DATA.dat&quot;;
  
  public void init(final Context context) {
      this.appContext = context;
  
      dataFile = new File(appContext.getFilesDir(), FILE_NAME);
  
      if (dataFile.exists()) {
          loadData();
  
      } else {
  
          try {
              dataFile.createNewFile();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          arrayList = new ArrayList &lt; MyCustomObject &gt; ();
  saveData();
     }
  
  }
  
  
  private void saveData() {
      FileOutputStream fos = null;
  
      try {
          fos = new FileOutputStream(dataFile);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } finally {
  
          if (fos != null) {
  
              ObjectOutputStream oos = null;
              try {
  
                  oos = new ObjectOutputStream(fos);
  
                  if (oos != null) {
  
                      oos.writeObject(arrayList);
  
                  }
                  assert oos != null;
                  oos.close();
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
  
  }
  
  private void loadData() {
  
      FileInputStream fis = null;
      try {
          fis = new FileInputStream(dataFile);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } finally {
  
          if (fis != null) {
  
              ObjectInputStream ois = null;
              try {
                  ois = new ObjectInputStream(fis);
  
                  if (ois != null) {
  
                      try {
  
                          arrayList = (ArrayList &lt; MyCustomObject &gt; ) ois.readObject();
                      } catch (ClassNotFoundException e) {
                          e.printStackTrace();
                      }
                  } else {
  
                  }
                  assert ois != null;
                  ois.close();
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
  }
  </code></pre>
  <p><strong>Here is MyCustomObject</strong></p>
  <pre><code>public class MyCustomObject implements Serializable {
  
      public String myname = &quot;&quot;;
      public String someOtherItem = &quot;&quot;;
      public int aNumber = 0;
      public MyCustomObject getCustomObject() {
          return this;
      }
  }
  </code></pre>