Domanda

Im having a problem when I try to deserialize an ArrayList, the .readObject() method is returning a boolean instead of an object, and I have no clue why this is happening. Thank you and sorry for the bad english.

Class to be serialized

import java.io.Serializable;

public class Pessoa implements Serializable {

public String nome;
public String sobrenome;
public int idade;

public Pessoa(String n, String sn, int i){
    this.nome = n;
    this.sobrenome = sn;
    this.idade = i;
}
}

Serialization and Deserialization

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TESTES {

public static void main(String[] args) {

    Pessoa p1 = new Pessoa("Tony", "Stark", 35);
    ArrayList<Pessoa> listaPessoas = new ArrayList<>();

    try {
        FileOutputStream fos = new FileOutputStream("obj.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(listaPessoas.add(p1));
        oos.flush();
        oos.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(TESTES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(TESTES.class.getName()).log(Level.SEVERE, null, ex);
    }


    try {
        FileInputStream fis = new FileInputStream("obj.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);

        //===== HERES THE PROBLEM =========================
        listaPessoas = (ArrayList<Pessoa>) ois.readObject();
        //===== HERES THE PROBLEM =========================

        ois.close();

        System.out.println(listaPessoas.get(0).nome);

    } catch (FileNotFoundException ex) {
        Logger.getLogger(TESTES.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(TESTES.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

The Exception

Exception in thread "main" java.lang.ClassCastException: java.lang.Boolean cannot be          cast to java.util.ArrayList
at testes.TESTES.main(TESTES.java:41)
Java Result: 1
È stato utile?

Soluzione 2

Your problem is in the serialization:

oos.writeObject(listaPessoas.add(p1));

this writes the return value of List.add() which is a boolean (true if the collection is changed by the operation) which is then boxed in a Boolean.

When you then deserialize:

 listaPessoas = (ArrayList<Pessoa>) ois.readObject();

a Boolean is found in the stream, but you try to cast it to List, generating the class cast exception.

Just keep the serialization of the list in a single statements like:

 listaPessoas.add(value);
 [...] // other operation on the list
 oos.writeObject(listaPessoas);

Altri suggerimenti

Your problem is in this line:

oos.writeObject(listaPessoas.add(p1));

The Java Collections API is not fluent. The add method doesn't return the List, but instead a boolean indicating whether the operation was successful. So you should split above line into two like this:

listaPessoas.add(p1);
oos.writeObject(listaPessoas);

Write the ArrayList to the ObjectOutputStream and not the return value of the add method.

 listaPessoas.add(p1)
 oos.writeObject(listaPessoas);

You are serializing the boolean variable oos.writeObject(listaPessoas.add(p1)); -- add()return value is boolean.

You should do like this. listaPessoas.add(p1); oos.writeObject(arraylistobject);

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top