title: Java serialization - java.lang.ClassCastException Error
body: <p>I have a <strong>StorageImpl</strong> class which is serializable. This class has an inner class <strong>Record</strong>. </p>
  
  <pre><code>public class StorageImpl &lt;ID, T extends IDataStore&lt;ID&gt;&gt; implements IStorage &lt;T, ID&gt;
  {
  
      private class Record implements Serializable
      {
          private static final long serialVersionUID = 1L;
  
          private Long key;
          private List&lt;T&gt; dataList;
  
          private Record()
          {
              key      = new Long(0);
              dataList = new ArrayList&lt;T&gt;();
          }
      }
  }
  </code></pre>
  
  <p>I want to store and retrieve a <strong>Record</strong> which in turn has   </p>
  
  <pre><code>private Long key;
  private List&lt;T&gt; dataList;
  </code></pre>
  
  <p>Once I retrieve a Record, I can get all the books in <strong>bookList</strong> member of the Record.  </p>
  
  <pre><code>public class StorageImpl &lt;ID, T extends IDataStore&lt;ID&gt;&gt; implements IStorage &lt;T, ID&gt;
  {
      private class Record implements Serializable
      {
          private static final long serialVersionUID = 1L;
  
          private Long key;
          private List&lt;T&gt; dataList;
  
          private Record()
          {
              key      = new Long(0);
              dataList = new ArrayList&lt;T&gt;();
          }
  
      private String dataFile;
      private Record record;
  
      public StorageImpl(String dataFile)
      {
          this.dataFile = dataFile;
          record        = new Record();
      }
  
  
      public List&lt;T&gt; retrieve() throws Exception
      {
          List&lt;T&gt; dataList = new ArrayList&lt;&gt;();
  
          File file = new File(dataFile);
  
          if (!file.exists())
          {
              return dataList;
          }        
  
          try (FileInputStream fis = new FileInputStream(dataFile); ObjectInputStream ois = new ObjectInputStream(fis))
          {
              record = (Record) ois.readObject();
          }
          catch (IOException e)
          {
              e.printStackTrace();
          }
          catch (ClassNotFoundException e)
          {
              e.printStackTrace();
          }
  
          for (int i = 0; i &lt; record.dataList.size(); i++) 
          {
              dataList.add(record.dataList.get(i));
          }
  
          return dataList;
      }
      @SuppressWarnings("unchecked")
      @Override
      public void store(T t) throws Exception
      {
         List&lt;T&gt; dataList = retrieve();
  
         ++record.key;        
         t.setID((ID) record.key);
         dataList.add(t);
  
         record.dataList = dataList;
                 writeData(record);
      }
      private void writeData(Record record)
      {
          try (FileOutputStream fos = new FileOutputStream(dataFile);
               ObjectOutputStream oos = new ObjectOutputStream(fos))
          {
              oos.writeObject(record);
          }
          catch (IOException e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Update:</p>
  
  <p>Please note I want to persist just a <strong>Record</strong> and not a <strong>List</strong>.</p>
  
  <p>The error after code changes looks like relates to <strong>Record</strong> not being serializable:</p>
  
  <p>Caused by: java.io.NotSerializableException: </p>
  
  <pre><code>atjava.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
  at StorageImpl.writeData(StorageImpl..java:159)
  </code></pre>
  
  <p>And this is the call inside writeData() at line 159  </p>
  
  <pre><code>oos.writeObject(record);
  </code></pre>