'Reading objects with ObjectInputStream

I have a problem with ObjectInputStream , I want to read objects that I previously wrote with ObjectOutputStream. As i have a lot of objects I decided to use available() and readObject() in a while loop , but do not work.When I was debugging I noticed that available() return 0 but the target file is not empty. I do not know what I do wrong so I will apreciate a bit of help. Thank you :)

Code :

 public static Set<Contacto> contactosFromSerial(){
        Set<Contacto> agenda = null;
        try(ObjectInputStream obj = new ObjectInputStream(new FileInputStream("data/object.bin"))){
            agenda = new TreeSet<>();
            Contacto c ;
            
            while(obj.available() > 0){
                agenda.add((Contacto)obj.readObject());
            }
            return agenda;
    
        }catch(EOFException e){
            return agenda;


        }catch(IOException e){
            return null;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }  

UPDATE 1 : I implement Serializable



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source