title: Class Cast Exception when DeSerializing an Object
body: <p><strong>Overview</strong></p>
  
  <p>I am new to Android development and I am trying to serialize and then deserialize the value of an object but I am getting exception.</p>
  
  <p><strong>Exception</strong></p>
  
  <p><strong>FATAL EXCEPTION: main</strong></p>
  
  <p><strong>java.lang.ClassCastException: java.io.File cannot be cast to com.testingapplication.SchoolNameDetails
  com.testingapplication.fragment.HomeFragment$1.onClick(HomeFragment.java:62)</strong> </p>
  
  <p><strong>What I tried</strong></p>
  
  <p>SchoolNameDetails class is as follows:</p>
  
  <pre><code>public class SchoolNameDetails implements Serializable {
  private int id;
  private String schoolname;
  private String level;
  private String pkno;
  private String nano;
  private String ucname;
  private String officeno;
  private static final long serialVersionUID = 46548923;
  
  public SchoolNameDetails(){}
  
  public SchoolNameDetails(String schoolname, String level, String pkno,String nano, String ucname, String officeno){
      this.schoolname=schoolname;
      this.level=level;
      this.pkno=pkno;
      this.nano=nano;
      this.ucname=ucname;
      this.officeno=officeno;
  }
  
  public SchoolNameDetails(int id, String schoolname, String level, String pkno,String nano, String ucname, String officeno){
      this.id=id;
      this.schoolname=schoolname;
      this.level=level;
      this.pkno=pkno;
      this.nano=nano;
      this.ucname=ucname;
      this.officeno=officeno;
  }
  
  public int getId() {
      return id;
  }
  public void setId(int id) {
      this.id = id;
  }
  public String getSchoolname() {
      return schoolname;
  }
  public void setSchoolname(String schoolname) {
      this.schoolname = schoolname;
  }
  public String getLevel() {
      return level;
  }
  public void setLevel(String level) {
      this.level = level;
  }
  public String getPkno() {
      return pkno;
  }
  public void setPkno(String pkno) {
      this.pkno = pkno;
  }
  public String getNano() {
      return nano;
  }
  public void setNano(String nano) {
      this.nano = nano;
  }
  public String getUcname() {
      return ucname;
  }
  public void setUcname(String ucname) {
      this.ucname = ucname;
  }
  public String getOfficeno() {
      return officeno;
  }
  public void setOfficeno(String officeno) {
      this.officeno = officeno;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>and here I am serializing the object</strong></p>
  
  <pre><code>@Override
  protected void onStop() {
      super.onStop();
      SchoolNameDetails details = new SchoolNameDetails();
      details.setSchoolname(t2.getText().toString());
      details.setLevel(t2a.getText().toString());
      details.setPkno(t5.getText().toString());
      details.setNano(t6.getText().toString());
      details.setUcname(t7.getText().toString());
      details.setOfficeno(t8.getText().toString());
  
      saveObject(details);
  
  }
  
  public void saveObject(SchoolNameDetails d) {
      try
      {
          ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/sdcard/save_object.ser")));
          oos.flush();
          oos.close();
      }
      catch(Exception ex)
      {
          Log.v("Error",ex.getMessage());
          ex.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p><strong>and Here I am deserializing the object in my Fragment class</strong></p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_home,
              container, false);
      Button b1= (Button) view.findViewById(R.id.button1);
      final TextView t1 = (TextView) view.findViewById(R.id.textView1);
      b1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              SchoolNameDetails details = (SchoolNameDetails)loadSerializedObject(new File("/sdcard/save_object.ser"));
              System.out.println("Name : " + details.getSchoolname());
          }
  
  
      });
  
      return view;
  }
  public Object loadSerializedObject(File f)
  {
      try
      {
          ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
          Object o = ois.readObject();
  
      }
      catch(Exception ex)
      {
          Log.v("Error : ","" + ex.getMessage());
          ex.printStackTrace();
      }
      return f;
  }
  </code></pre>
  
  <p>Now the last return value gives exception. i tried with return null but then it gives null pointer exception.<strong>Please Help I am stuck</strong> in this for hours and haven't found any solution</p>