0

I'm encountering the following EOFException:

java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2903)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1502)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at Practica.Animalario.Poblacion.abrirPoblacion(Poblacion.java:37)
at InterfazGrafica.Ventana.actionPerformed(Ventana.java:122)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

While executing this code that I've written. The purpose of this code is to read / write the objects into a file:

public class Poblacion implements Serializable{
    public Raton poblacion [];
    public String nombrePoblacion;
    public String responsable;
    public int dias;

    public Poblacion (){
        poblacion = null;
        nombrePoblacion = "";
        responsable = "";
        dias = 0;
    }

    public Poblacion (String nombre, String cientifico, int _dias){
        poblacion = null;
        nombrePoblacion = nombre;
        responsable = cientifico;     
        dias = _dias;
    }

public class Raton {
    String codigoReferencia;
    Date fechaDeNacimiento;
    int peso;
    String sexo;
    float temperatura;
    String observaciones;
    char X1;
    char X2;
    char Y1;

    public void Raton(String _codigo, Date _fecha, int _peso, String _sexo, float _temp, String _obs, char _x1, char _x2){
        codigoReferencia = _codigo;
        fechaDeNacimiento = _fecha;
        peso = _peso;
        sexo = _sexo;

        if (sexo.equals("Macho")) {
            X1=_x1;
            Y1=_x2;                    
        } else {
            X1=_x1;
            X2=_x2;
        }

        temperatura=_temp;
        observaciones=_obs;       
    }

    public String toString(){
        return codigoReferencia+fechaDeNacimiento+peso+sexo+temperatura+X1+X2+Y1+observaciones;
    }
}



    public static Poblacion abrirPoblacion (File file) throws ClassNotFoundException, IOException {
        File archivo = file;
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        Poblacion poblacion=new Poblacion();

        try {
            fileInputStream = new FileInputStream(archivo);
            objectInputStream = new ObjectInputStream(fileInputStream);
            System.out.println(objectInputStream.readObject());
            poblacion = (Poblacion) objectInputStream.readObject();
        } catch(IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (objectInputStream!=null) {
                    objectInputStream.close();
                }
            } catch(IOException ex) {
                ex.printStackTrace();
            }
            try {
                if (fileInputStream!=null) {
                    fileInputStream.close();
                }
            } catch(IOException ex) {
                ex.printStackTrace();
            }
            return poblacion;
        }
    }



    public static void crearPoblacion (String _nombre,String _responsable, int _dias) {
        File archivo = new File("C:\\Users\\rober\\OneDrive - Fundación Universitaria San Pablo CEU\\Java\\Práctica 1"+"\\"+_nombre+".dat");
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        Poblacion nuevaPoblacion = new Poblacion(_nombre,_responsable,_dias);

        try {
            fileOutputStream = new FileOutputStream(archivo);
            objectOutputStream = new ObjectOutputStream(fileOutputStream); 
            objectOutputStream.writeObject(nuevaPoblacion);
        } catch(IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (objectOutputStream != null) {
                    objectOutputStream.close();
                }
            } catch(IOException ex) {
                ex.printStackTrace();
            }

            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch(IOException ex) {
                ex.printStackTrace();
            }
        }
    }
1
  • This has exactly nothing to do with streamreader or streamwriter. Don't tag indiscriminately. Commented Mar 31, 2017 at 9:05

1 Answer 1

1

You are calling readObject() twice when only one object is in the file.

System.out.println(objectInputStream.readObject());
poblacion=(Poblacion) objectInputStream.readObject();

Obviously, it reaches the end of the file and can no longer read the second one. Simply change this so that you don't try to read it twice:

poblacion=(Poblacion) objectInputStream.readObject();
System.out.println(poblacion);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.