Open file with an object that stores another object (ArrayList wrapper)

0

Good, I am having problems to be able to advance with a project, the question is that I have an 'Exhibition' class, which in turn has a 'Works' field that is an ArrayList package. When I save my class Exposure does not jump any type of error, it generates the file to me apparently well (if I add or delete content and I return to save it varies the size of the file). And when I open it again, it seems to open the Exposure class, but when I check the contents of the ArrayList, I verify that it is not loading the data that should be from that file. Here I leave the fragments of the code, a greeting and thanks in advance. (Both the Exposure class and the ArrayList wrapper implement the Serializable

public class Exposicion implements Serializable{
    private Obras museo = Obras.getInstance(); // Fondos del museo.
    ...

Selecting the file from the JFileChooser - >

private void abrirFichero() throws FileNotFoundException,
        ClassNotFoundException, IOException {
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.addChoosableFileFilter(filtro);
    if (fileChooser.showDialog(fileChooser, "Abrir Fichero") == JFileChooser.APPROVE_OPTION) {
        Fichero.FICHERO = fileChooser.getSelectedFile();
        exposicion = (Exposicion) Fichero.abrir(fileChooser.getSelectedFile());
        frame.setTitle(Fichero.getFichero().getName());
        JOptionPane.showMessageDialog(null, "Cargado con exito");
    ....

Opening the File - >

public static Object abrir(File archivo) throws IOException, ClassNotFoundException {
    try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(archivo))) {
        return in.readObject();
    }
}

EDITO - > I have tried to modify the field Works once I load the file, but in this way it does not load the content in the wrapper:

exposicionAux = (Exposicion) Fichero.abrir(fileChooser.getSelectedFile());
exposicion = exposicionAux;
exposicion.setMuseo(exposicionAux.getMuseo());

SOLVED - >

The error was in the wrapper of ArrayList , because it had declared it static

private static ArrayList<ObraDeArte> obras;

and the static elements can not be serialized.

private ArrayList <ObraDeArte> obras;
    
asked by Pablo León 26.05.2017 в 11:08
source

1 answer

0
  

Both the Exposure class and the ArrayList wrapper implement   the Serializable

The elements that you save in the ArrayList must also implement the serializable class, or they will be ignored when saving them in a file.

    
answered by 29.05.2017 в 16:50