简体   繁体   中英

Class Cast Exception when DeSerializing an Object

Overview

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.

Exception

FATAL EXCEPTION: main

java.lang.ClassCastException: java.io.File cannot be cast to com.testingapplication.SchoolNameDetails com.testingapplication.fragment.HomeFragment$1.onClick(HomeFragment.java:62)

What I tried

SchoolNameDetails class is as follows:

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;
}

}

and here I am serializing the object

@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();
    }

}

and Here I am deserializing the object in my Fragment class

@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;
}

Now the last return value gives exception. i tried with return null but then it gives null pointer exception. Please Help I am stuck in this for hours and haven't found any solution

First of all change your saveObject method to actually write the object:

public void saveObject(SchoolNameDetails d) {
    try
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("/sdcard/save_object.ser")));
        //write to ObjectOutputStream
        oos.writeObject(d);
        oos.flush();
        oos.close();
    }
    catch(Exception ex)
    {
        Log.v("Error",ex.getMessage());
        ex.printStackTrace();
    }
}

Next change your loadSerializedObject method to return the object from the input stream:

public SchoolNameDetails loadSerializedObject(File f)
{
    try
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
        SchoolNameDetails o = (SchoolNameDetails ) ois.readObject();
        return o;
    }
    catch(Exception ex)
    {
        Log.v("Error : ","" + ex.getMessage());
        ex.printStackTrace();
    }
    return null;
}

Finally in your onClick make sure the object is not null:

    @Override
    public void onClick(View view) {
        SchoolNameDetails details = loadSerializedObject(new File("/sdcard/save_object.ser"));
        if (details != null) {
            System.out.println("Name : " + details.getSchoolname());
        }
    }

You're returning f at the end of the function. But you have to return o (what you've got from method ois.readObject().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM