4

I am trying to read all objects from the file. Below is the snippet of the method use to fetch list of object from file.

public List displayParties() {
        ObjectInputStream ois = null;
        List<RegisterParty> results = new ArrayList();
        try {
            FileInputStream fis = new FileInputStream("/media/user/disk2/myapp/assignment/party.ser");
            ois = new ObjectInputStream(fis);
            while (true) {
                System.out.println(" inside while true");
                results.add((RegisterParty) ois.readObject());
                System.out.println(results);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        catch (Exception ex){
                ex.printStackTrace();

        } finally{
            try {
                ois.close();
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }
        return results;
    }

RegisterParty Class :

public class RegisterParty implements Serializable {
    String bookingPersonName;
    String childName;
    String childAge;
    String theme;
    String foodAlergies;
    String noOfGuest;
    String specialGuest;

    public String getBookingPersonName() {
        return bookingPersonName;
    }

    public void setBookingPersonName(String bookingPersonName) {
        this.bookingPersonName = bookingPersonName;
    }

    public String getChildName() {
        return childName;
    }

    public void setChildName(String childName) {
        this.childName = childName;
    }

    public String getChildAge() {
        return childAge;
    }

    public void setChildAge(String childAge) {
        this.childAge = childAge;
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    public String getFoodAlergies() {
        return foodAlergies;
    }

    public void setFoodAlergies(String foodAlergies) {
        this.foodAlergies = foodAlergies;
    }

    public String getNoOfGuest() {
        return noOfGuest;
    }

    public void setNoOfGuest(String noOfGuest) {
        this.noOfGuest = noOfGuest;
    }

    public String getSpecialGuest() {
        return specialGuest;
    }

    public void setSpecialGuest(String specialGuest) {
        this.specialGuest = specialGuest;
    }

    public String toString(){
        return bookingPersonName+" "+childName+" "
                +childAge+" "+foodAlergies+" "+theme+" "+noOfGuest+" "+specialGuest;
    }
}

But getting the below exception :

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: models.RegisterParty
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at helper.HelperFunctions.displayParties(HelperFunctions.java:39)
    at services.PartyOperations.listAllParties(PartyOperations.java:64)
    at assignment.App.main(App.java:34)
    at helper.HelperFunctions.saveParty(HelperFunctions.java:24)
    at services.PartyOperations.registerParty(PartyOperations.java:53)
    at assignment.App.main(App.java:28)
Caused by: java.io.NotSerializableException: models.RegisterParty
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    at helper.HelperFunctions.saveParty(HelperFunctions.java:20)
    at services.PartyOperations.registerParty(PartyOperations.java:52)
    ... 1 more

RegisterParty implements the serializable interface. Its a menu based app, so when i save the object in file, its save successfully. But call the method to get all object in list, its throw the exception. Any Idea why?

This method successfully execute to serialize the object:

public void saveParty(RegisterParty registerParty){
    try {
        FileOutputStream fout = new FileOutputStream("/media/user/disk2/myapp/assignment/party.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(registerParty);
        oos.close();
        System.out.println("Successfull Register");
        System.out.println("========Select you choice========");
        App.main(str);
    }
    catch (Exception ex){
            ex.printStackTrace();
    }
}
Vivek Dhiman
  • 1,967
  • 6
  • 46
  • 82

2 Answers2

0

When I implement a Serializable object I add a static variable serialVersionUID to the class:

private static final long serialVersionUID = -1892561327013038124L;

This line gets added as a suggestion by either the eclipse or android studio. I don't know how to do it with IntelliJIdea.

It worth a try! I believe this ID used for deserialisation.

hasan
  • 23,815
  • 10
  • 63
  • 101
  • 2
    No, it's not, this id is to keep track of versions of the class for serial and deserialization (you don't want a corrupt copy in memory if you deserialize the wrong version). You don't even need it because the compiler will create a default if none is supplied... http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it – J Atkin Apr 29 '15 at 15:06
0

There was conflict in fileToRead & fileToWrite.

    FileInputStream fis = new FileInputStream("/media/user/disk2/myapp/assignment/party.ser");
    FileOutputStream fout = new FileOutputStream("/media/user/disk2/myapp/assignment/party.txt");
Mike G
  • 4,232
  • 9
  • 40
  • 66
Vivek Dhiman
  • 1,967
  • 6
  • 46
  • 82