title: Type safety: The expression of type ArrayList needs unchecked conversion to conform to ArrayList<Student>? This throws a ClassCastException
body: <p>I'm still quite new to Java and need a hand working out how to get around my problem.</p>
  <p>I'm attempting to write a method to deserialize an object that I have called <code>Student</code>. The method is as follows:</p>
  <pre><code>   public void readStudentInfo() {
              // Desrializes the student objects into an ArrayList for you to compare against
  
              try{
                  FileInputStream fis = new FileInputStream(&quot;student.ser&quot;);
                  ObjectInputStream ois = new ObjectInputStream(fis);
  
                  deserializedStudents = (ArrayList) ois.readObject();
  
                  ois.close();
                  fis.close();
  
              }
              catch(IOException ioe){
                  ioe.printStackTrace();
                  return;
              } catch(ClassNotFoundException c){
                  System.out.println(&quot;Class not found&quot;);
                  c.printStackTrace();
                  return;
              }
  
              for(Student student : deserializedStudents){
                  System.out.println(student.toString());
              }
              }
  </code></pre>
  <p>The class Student that it works with is as follows:</p>
  <pre><code>import java.io.Serializable;
  
  public class Student implements Serializable{
      
      private static final long serialVersionUID = 1L;
  
      // Class Attributes
      private String studentID;
      private String rankings;
      private char personalityType;
      private String conflict;
      
      private String preferences;
  
      
      // Class Constructor
  
      public Student(String ID){
          this.studentID = ID;
      }
      
      public Student(String ID, String grades) {
          this.studentID = ID;
          grades = grades.trim();
          this.rankings = grades;
      }
  
      public Student(String ID, String ranking,char personality){
          this.studentID = ID;
          this.rankings = ranking;
          this.personalityType = personality;
      }
      
      // Accessor Methods
      public String getStudentID() {
          return this.studentID;
      }
  
      public String getRankings(){
          return this.rankings;
      }
  
      public String getPreferences(){
          return this.preferences;
      }
  
      public char getPersonalityType(){
          return this.personalityType;
      }
  
      public String getConflict(){
          return this.conflict;
      }
  
      //Modifier Methods
  
      public boolean setPreferences(String pref){
          this.preferences = pref;
          return true;
      }
  
      public boolean setGrades(String grades){
          this.rankings = grades;
          return true;
      }
  
      public boolean setPersonalityType(char pers){
          this.personalityType = Character.toUpperCase(pers);
          return true;
      }
  
      public boolean setConflict(String ids){
          this.conflict = ids;
          return true;
      }
  
      @Override
  
      public String toString(){
          return studentID + &quot;;&quot; + rankings + &quot;;&quot; + personalityType + &quot;;&quot; + conflict + &quot;;&quot; + preferences; 
      }
      
  
      
  }
  
  
  </code></pre>
  <p>The ArrayList <code>deserializedStudents</code> is initiated at the top of the class like so: <code>ArrayList&lt;Student&gt; deserializedStudents = new ArrayList&lt;&gt;();</code></p>
  <p>For some reason the following line of <code>readStudentInfo()</code> gives this warning : Type safety: The expression of type ArrayList needs unchecked conversion to conform to ArrayListJava(16777748)</p>
  <p>If I ignore it and run the program regardless, the <code>readStudentInfo()</code> method gives the following error: <code>Exception in thread &quot;main&quot; java.lang.ClassCastException: class Student cannot be cast to class java.util.ArrayList (Student is in unnamed module of loader 'app'; java.util.ArrayList is in module java.base  of loader 'bootstrap')</code></p>
  <p>Can someone please help me to understand why this is happening? I've tried changing the cast in the following line a few times: <code>deserializedStudents = (ArrayList) ois.readObject();</code>. I've also tried changing things around a little where I initiate the ArrayList at the start of the program, but no luck yet.</p>
  <p>Clarification: The basic idea here is that I'm trying to deserialize a bunch of Student type objects into an ArrayList so that I can use them again.</p>
  <p>Please help!</p>