• Viernes 3 de Mayo de 2024, 12:47

Autor Tema:  Error con archivos y ArrayList  (Leído 1674 veces)

tomas_sauron

  • Miembro activo
  • **
  • Mensajes: 56
    • Ver Perfil
Error con archivos y ArrayList
« en: Miércoles 13 de Abril de 2011, 07:06 »
0
hola gente!!! estoy empezando en java hace un mes y algo mas y leyendo un poco logre llegar  hasta archivos en mi entendimiento pero m falta mucho todavia.Bueno la cosa es esta , tengo problemas con este codigo estoy intentando a hacer una especie d agenda pero lo unico q m importa ees ver el funcionamiento de archivos y ArrayList y su relacion el programa esta hecho en netbeans 6.9
y el problema salta cuando quiero leer el archivo paara mostrar los datos al guardar no hay problema el error qu tira es en tiempo d ejecucion y es est
[color=#FF0000Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException]
aqui el codigo del boton q abre los datos
Código: Java
  1.  private void btnSavePerson1ActionPerformed(java.awt.event.ActionEvent evt) {
  2.  
  3.       try{
  4.           ObjectInputStream fileIn=new ObjectInputStream(new FileInputStream("agenda.dat"));
  5.             try {
  6.                 book = (ArrayList) fileIn.readObject();
  7.             } catch (ClassNotFoundException ex) {
  8.                 Logger.getLogger(Formulario.class.getName()).log(Level.SEVERE, null, ex);
  9.             }
  10.           fileIn.close();
  11.          Datos agenda=book.get(i-1);
  12.          nom=agenda.getNombre();
  13.          ape=agenda.getApellido();
  14.          dir=agenda.getDireccion();
  15.          mail=agenda.getEmail();
  16.          tel=agenda.getTelefono();
  17.          txtName1.setText(nom);
  18.          txtApe1.setText(ape);
  19.          txtDire1.setText(dir);
  20.          txtmail1.setText(mail);
  21.          txtTel1.setText(""+tel);
  22.       }catch(FileNotFoundException ex){
  23.           JOptionPane.showMessageDialog(this,"Error!! No se encuentra archivo");
  24.  
  25.       }catch(IOException ex){
  26.           JOptionPane.showMessageDialog(this,"Error!!");
  27.  
  28.       }
  29.  
  30.  
  31.     }
  32.  
  33.  
y el boton qu guarda es este
Código: Java
  1.  private void btnSavePersonActionPerformed(java.awt.event.ActionEvent evt) {
  2.  
  3.            ape=txtApe.getText();
  4.            nom=txtName.getText();
  5.            dir=txtDire.getText();
  6.            mail=txtmail.getText();
  7.            tel=Integer.parseInt(txtTel.getText());
  8.            Datos agenda = new Datos(nom,ape,mail,dir,tel);
  9.            book.add(i,agenda);
  10.            i++;
  11.            txtApe.setText("");
  12.            txtName.setText("");
  13.            txtDire.setText("");
  14.            txtmail.setText("");
  15.            txtTel.setText("");
  16.  
  17.            try{
  18.                ObjectOutputStream fileOut=new ObjectOutputStream(new FileOutputStream("agenda.dat"));
  19.                fileOut.writeObject(agenda);
  20.                fileOut.close();
  21.            }catch(FileNotFoundException ex){
  22.                JOptionPane.showMessageDialog(this,"Error!! No se encuentra archivo");
  23.            }catch(IOException ex){
  24.               JOptionPane.showMessageDialog(this,"Error!!");
  25.  
  26.  
  27.            }
  28.  
  29.  
  30.     }
  31.  
PD: hay una cclase Datos q possee todos los geters y seters con los atributos nombre apellido direccion email y telefono si alguien m puede contestar se lo agradecere mucho !!!!!!!!!!!!


[/color]