Dúvida com Serialização

0 respostas
A

Galera, alguém pode me dizer onde está o erro nesta pequena implementação?

package model;

import java.io.Serializable;

public class Vesa implements Serializable{
    private int horizontal;
    private int vertical;
    private int id;

    public Vesa(int horizontal, int vertical, int id) {
        this.horizontal = horizontal;
        this.vertical = vertical;
        this.id = id;
    }

    public Vesa() {}

    public int getHorizontal() {return horizontal;}
    public void setHorizontal(int horizontal) {this.horizontal = horizontal;}
    
    public int getVertical() {return vertical;}
    public void setVertical(int vertical) {this.vertical = vertical;}

    public int getId() {return id;}
    public void setId(int id) {this.id = id;}
    
    
}
package Controler;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import model.Vesa;

public class VesaDAO implements Serializable{
    File flvesa = new File("vesa.ser");
    
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    
    List<Vesa> Lista = null;
    
    public void Inserir(Vesa vesa) throws FileNotFoundException, IOException, ClassNotFoundException{
        if(flvesa.exists()){
            Lista = ListarTodos();
            Lista.add(vesa);
            ArmazenaAlteracao(Lista);
            JOptionPane.showMessageDialog(null, "Inserido com sucesso!");
        }else{
            fos = new FileOutputStream(flvesa);
            oos = new ObjectOutputStream(fos);
            Lista = new ArrayList();
            Lista.add(vesa);
            oos.writeObject(Lista);
            JOptionPane.showMessageDialog(null, "Inserido com sucesso!");
        }  
    }
    
    public void Excluir(int id) throws FileNotFoundException, IOException, ClassNotFoundException{
        if(flvesa.exists()){
            Lista = ListarTodos();
            
            for(Vesa v: Lista){
                if(v.getId()==id){
                    Lista.remove(v);
                    ArmazenaAlteracao(Lista);
                    JOptionPane.showMessageDialog(null, "Excluído com sucesso!");
                }
            }
        }else{
            JOptionPane.showMessageDialog(null, "Não foi possível excluir!");
        }
    }
    
    public void Alterar(Vesa vesa) throws FileNotFoundException, IOException, ClassNotFoundException{
        if(flvesa.exists()){
            Lista = ListarTodos();
            
            for(Vesa v: Lista){
                if(v.getId()==vesa.getId()){
                    Lista.get(Lista.indexOf(v)).setHorizontal(vesa.getHorizontal());
                    Lista.get(Lista.indexOf(v)).setVertical(vesa.getVertical());
                    ArmazenaAlteracao(Lista);
                    
                    JOptionPane.showMessageDialog(null, "Alterado com sucesso!");
                }
            }
        }else{
            JOptionPane.showMessageDialog(null, "Não foi possível alterar!");
        }
    }

    public List<Vesa> ListarTodos() throws FileNotFoundException, IOException, ClassNotFoundException {
        List<Vesa> Lista = null;
        if(flvesa.exists()){
            fis = new FileInputStream(flvesa);
            ois = new ObjectInputStream(fis);
            Lista = (List<Vesa>) ois.readObject();
        }
        return Lista;
    }

    private void ArmazenaAlteracao(List<Vesa> Lista) throws FileNotFoundException, IOException {
            oos = new ObjectOutputStream(new FileOutputStream(flvesa));
            oos.writeObject(Lista);
    }
}
package suportetv;

import Controler.VesaDAO;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import model.Vesa;

public class SuporteTV implements Serializable{

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        VesaDAO vdao = new VesaDAO();
        Vesa v1 = new Vesa(75, 75, 1);
        Vesa v2 = new Vesa(100, 100, 2);
        Vesa v3 = new Vesa(200, 200, 3);
        Vesa v4 = new Vesa(200, 100, 4);
        Vesa v5 = new Vesa(300, 200, 5);
        
        vdao.Inserir(v1);
        vdao.Inserir(v2);
        vdao.Inserir(v3);
        vdao.Inserir(v4);
        vdao.Inserir(v5);
        
        List<Vesa> vesa = vdao.ListarTodos();
    
        for(Vesa v: vesa){
            System.out.println(v.getId()+"  "+v.getHorizontal()+" x "+v.getVertical());
        }
    }
    
    
}

erro:

run:
Exception in thread "main" java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: model.Vesa
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at java.util.ArrayList.readObject(ArrayList.java:733)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1004)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at Controler.VesaDAO.ListarTodos(VesaDAO.java:75)
at Controler.VesaDAO.Inserir(VesaDAO.java:22)
at suportetv.SuporteTV.main(SuporteTV.java:20)
Caused by: java.io.NotSerializableException: model.Vesa
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at java.util.ArrayList.writeObject(ArrayList.java:710)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at Controler.VesaDAO.Inserir(VesaDAO.java:30)
at suportetv.SuporteTV.main(SuporteTV.java:13)
Java Result: 1

Criado 1 de outubro de 2012
Respostas 0
Participantes 1