0

I am trying to save/load instances of my TicketSet class in Java. Below is the class and the class variables. The Ticket and Variable class are also Serializable.

public class TicketSet implements Serializable{
    public final int setID;
    public int ticketNum;
    public Ticket[] tickets;
    private static int xCount[];
    private static int yCount[];
    private static int zCount[];
    private Variable x;
    private Variable y;
    private Variable z;

In another class I save an instance of the TicketSet class which seems to work fine. In the code, gen is just an instance of a controller class which initialises TicketSet.

TicketSet set;
if (f.exists()) {
    FileOutputStream fileOut =new FileOutputStream(f,true);
    AppendingObjectOutputStream out = new AppendingObjectOutputStream(fileOut);
    set = gen.getTSet();
    out.writeObject(set);
    out.close();
    fileOut.close();
} else {
    FileOutputStream fileOut =new FileOutputStream(f,true);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    set = gen.getTSet();
    out.writeObject(set);
    out.close();
    fileOut.close();
}

To load the instances of TicketSet, I have the following code which throws the error.

ArrayList<Integer> tickid = new ArrayList<Integer>();
tSets = new HashMap<Integer, TicketSet>();
FileInputStream fileStr = null;
ObjectInputStream reader = null;
try { 
    fileStr = new FileInputStream("TicketSets.ser"); 
    reader = new ObjectInputStream(fileStr); 
    System.out.println(fileStr.available());
    TicketSet tSet= null;
    while (fileStr.available()>0) {
        Object next = reader.readObject(); //ERROR HERE
        if (next instanceof TicketSet) {
            tSet = (TicketSet) next;
            System.out.println("ID: "+tSet.setID);
            tSets.put(tSet.setID, tSet);
            tickid.add(tSet.setID);
        } else {
            System.out.println("Unexpected object type:  " + next.getClass().getName());
        }
    }
    //System.out.println("Size: "+tSets.size());
    reader.close();
    fileStr.close();
}
catch(IOException i) {
    i.printStackTrace();
}
catch (ClassNotFoundException c) {
    System.out.println("TicketSet class not found");
    c.printStackTrace();
}

The error thrown is:

ID: 7325825
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.io.ObjectStreamClass

So what I understand is:

  • The first TicketSet is loaded fine... which has ID=73225825
  • It is then trying to load an integer from the file rather than a TicketSet object.

Why is it trying to load an integer? Is there a way to skip reading anything other than objects? Should I try an alternative approach?

Edd
  • 3,724
  • 3
  • 26
  • 33
  • Apparently your input stream is not a valid serialized object. – Hot Licks Feb 10 '14 at 12:19
  • Do you have more than 1 Object in that file? Try to use something different from `available` - it most likely won't do what you expect. – Fildor Feb 10 '14 at 12:26
  • Maybe add jboss in your tags? http://docs.jboss.org/jbossas/javadoc/4.0.3SP1/common/org/jboss/util/stream/AppendingObjectOutputStream.html – Leo Feb 10 '14 at 13:22
  • or your AppendingObjectOutputStream is this hack? http://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream – Leo Feb 10 '14 at 13:25
  • @Fildor Yes I need it to work with multiple objects – tiraths27 Feb 10 '14 at 14:16
  • @Leo yes I am using that hack – tiraths27 Feb 10 '14 at 14:22
  • @tiraths27 have you saw the other answers warnings? – Leo Feb 10 '14 at 14:24
  • @Leo Thanks. I had another look and resolved it by: http://stackoverflow.com/questions/12279245/classcastexception-when-appending-object-outputstream/12438141#12438141 – tiraths27 Feb 10 '14 at 14:53
  • good. then please add your own answer to this question and accept it, so everybody will know it's solved :-) – Leo Feb 10 '14 at 15:30

1 Answers1

0

I was missing the Reset() in my AppendingObjectOutputStream.

ClassCastException when Appending Object OutputStream

Community
  • 1
  • 1