0

What I'm trying to do here, is to read some data from a MySQL database and the convert them into object data and serialize it in a file. Which apparently, it does. But I get a null in the console that I'm not able to understand where does it come from. If I check the error itself, it says it on this line:

pepi = (personita) ois.readObject();

The output you get is:

Betty, Laferez, 87.0 <- From database, ok.

Manolo, Lopez, 45.0 <- From database, ok.

Manuel, Rodriguez, 12.0 <- From database, ok.

Patricia, Alonso, 12.0 <- From database, ok.

null <- ???

So, could you can help me understanding why this line is causing the null problem?

public class practicabdyserializableobjetos {
public static void main(String[] args) throws ClassNotFoundException, IOException {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conexion = null;

    try {
        conexion = DriverManager.getConnection("jdbc:mysql://localhost/programacion", "root", "password");
        Statement sentencia = (Statement) conexion.createStatement();
        ResultSet resultado = sentencia.executeQuery("Select * from ejemplo");

        File persofiles = new File("./persofiles.txt");
        if (!persofiles.exists())
            persofiles.createNewFile();

        FileOutputStream fioust = new FileOutputStream(persofiles);
        ObjectOutputStream oboust = new ObjectOutputStream(fioust);
        int p=0;
        while (resultado.next()) {
            personita per = new personita();
            per.setNombre(resultado.getString(1));
            per.setApellido(resultado.getString(2));
            per.setEdad(resultado.getDouble(3));
            oboust.writeObject(per);
        }

        oboust.close();
        fioust.close();

        FileInputStream fis = new FileInputStream(persofiles);
        @SuppressWarnings("resource")
        ObjectInputStream ois = new ObjectInputStream(fis);

        while (true) {
            personita pepi = new personita();
            pepi = (personita) ois.readObject();
            System.out.println(pepi.getNombre() + ", " + pepi.getApellido() + ", " + pepi.getEdad());
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } 
    catch(EOFException e){
        System.out.println(e.getMessage());
    }

}

}

M.M.
  • 47
  • 8

1 Answers1

0

EDIT 2 - I Changed my answer

After reading this Question

Your code is correct. The null you are getting is the e.getMessage() inside the EOFException catch which, you could simply avoid by replacing the System.println statement with a ';').

}catch(EOFException e){
   ;
}

Against to what I thought you don't read a null value at the endo of the steram, so to know when your reached the end of the stream you use this EOFException. In the question I mention there is a consideration to make because it is in your hands to know if the EOF raise is because there are no more object to read, or because there was error and the stream was closed.

Juan
  • 5,525
  • 2
  • 15
  • 26
  • Can you replace System.out.println(e.getMessage()); by e.printStackTrace(); and tell me if it prints something different? – Juan May 27 '17 at 14:58
  • With my original code: java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at Tema8.PracticaBDmasobjetosserializasdos.practicabdyserializableobjetos.main(practicabdyserializableobjetos.java:53) (Line 53 is the same I highlighted above) – M.M. May 27 '17 at 15:04
  • To Edit 2: So, I think it is because there is no more objects to read. Then, It seems not a wrong code, but something i need it to make it work. Thanks for your help! – M.M. May 27 '17 at 16:46