Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid readObject exception, cannot cast String to ObjectStreamClass
    primarykey
    data
    text
    <p>I am working on an android project that loads data remotely, saves it into an array (if the data is new), writes it to disk as a serializeable, then reads it from disk to load an ArrayList.</p> <p>Sometimes the ArrayList populates with the data, sometimes it doesn't and the program crashes.</p> <p>I receive a runtime exception stating: java.land.ClassCastException: java.lang.String cannot be cast to java.io.ObjectStreamClass.</p> <p>Sometimes I also receive a java.io.StreamCorruptedException, and sometimes I receive and EOFException.</p> <p>Going through the exception tree, it seems to be originating from this call:</p> <pre><code>personsArray = (ArrayList&lt;Person&gt;) in.readObject(); </code></pre> <p>Now, sometimes the data loads fine without issues, most of the time the program crashes.</p> <p>Here is the code that saves the data to disk:</p> <pre><code>public static boolean saveFromRemoteSource(Context c, ArrayList&lt;?&gt; source){ //Save context context = c; //Save source to local file File file = context.getFileStreamPath(PERSONS_FILE); //Status if successful in saving boolean savedStatus = false; try { if(!file.exists()){ file.createNewFile(); }else{ //file already exists so don't do anything } //now load the data into the file FileOutputStream fos = context.openFileOutput(PERSONS_FILE, Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(source); oos.close(); savedStatus = true; } catch(IOException e){ e.printStackTrace(); savedStatus = false; } return savedStatus; } </code></pre> <p>Here is the code that reads the data from disk:</p> <pre><code> public static boolean loadPersonsArray(Context c){ context = c; boolean loadStatus = false; File file = context.getFileStreamPath(PERSONS_FILE); try{ if(!file.exists()){ file.createNewFile(); }else { //File is already created, do nothing } BufferedReader br = new BufferedReader(new FileReader(file)); if (br.readLine() != null) { FileInputStream fis = context.openFileInput(PERSONS_FILE); ObjectInputStream in = new ObjectInputStream(fis); personsArray = (ArrayList&lt;Person&gt;) in.readObject(); in.close(); fis.close(); loadStatus = true; } br.close(); } catch(IOException e){ e.printStackTrace(); Log.d("TAG", "IOException PERSONS_FILE file: " + e); loadStatus = false; } catch (ClassNotFoundException e) { e.printStackTrace(); Log.d("TAG", "ClassNotFoundException PERSONS_FILE file classnotfound: " + e); } return loadStatus; } </code></pre> <p>This is the Person class:</p> <pre><code> import java.io.Serializable; public class Person implements Serializable, Comparable&lt;Person&gt;{ //Person class private static final long serialVersionUID = 1L; private String personID; private String personName; private boolean displayPerson; //default constructor public Person(){ super(); } public Person(String personID, String personName, boolean displayPerson){ super(); this.personID = personID; this.personName = personName; this.displayPerson = displayPerson; } //Accessor Methods public String getPersonID(){ return personID; } public String getPersonName(){ return personName; } public boolean getDisplayPerson(){ return displayPerson; } //setter methods public void setPersonID(String personID){ this.personID = personID; } public void setPersonName(String personName){ this.personName = personName; } public void setDisplayPerson(boolean displayPerson){ this.displayPerson = displayPerson; } @Override public String toString(){ return this.getPersonName().replaceAll("[^A-Za-z0-9]", "") + this.getDisplayPerson(); } public int compareTo(Person otherPerson) { if(!(otherPerson instanceof Person)){ throw new ClassCastException("Not a valid Person object!"); } Person tempPerson = (Person)otherPerson; if(this.getPersonName().compareToIgnoreCase(tempPerson.getPersonName()) &gt; 0){ return 1; }else if(this.getPersonName().compareToIgnoreCase(tempPerson.getPersonName()) &lt; 0){ return -1; }else{ return 0; } } } </code></pre> <p>Where the data comes from to be written to the file</p> <pre><code> private void downloadPersons(){ HashMap&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); Kumulos.call("selectAllPersons", params, new ResponseHandler() { @Override public void didCompleteWithResult(Object result) { ArrayList&lt;Object&gt; personsList = new ArrayList&lt;Object&gt;(); for(Object o : (ArrayList&lt;?&gt;)result){ Person person = new Person(); person.setPersonID(replaceNandT((String) ((HashMap&lt;?,?&gt;) o).get("personID"))); person.setLawName(replaceNandT((String) ((HashMap&lt;?,?&gt;) o).get("personName"))); person.setDisplayLaw(stringToBool((String)((HashMap&lt;?,?&gt;) o).get("displayPerson"))); if(person.getDisplayPerson()==true){ personsList.add(person); } } //Save personsList to a file if(PersonsLoader.saveFromRemoteSource(context, personsList)){ updateVersionNumber(); isFinished=true; Log.d("TAG", "PersonsLoader.saveFromRemoteSource(context, personsList) success"); } } }); } </code></pre> <p>So what do you think is happening at this call?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload