Results 1 to 2 of 2
  1. #1
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default Serializing objects

    I'm trying to serialize java objects but i keep getting a list of errors. my program accepts multiple values and creates an instance of a class with them. the created object then gets stored in a StorageSystem class... it looks something like this

    Java Code:
        aCD = new CD(title, artist, playTime, numOfTracks);
        store.addItem(aCD);
    Then in the storage system i add the stored object to an arrayList... what i also want to do is add the object to a serialized file... my method for doing this is below... Am i going about this the wrong way. thanks...

    Java Code:
    public void addItem(Item hold)                      // adds object to the ArrayList
    {
        itemList.add(hold);
    
        totalStored++;
    
        try
        {
            FileOutputStream fout = new FileOutputStream("thequeue.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(hold);
            oos.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    This is the entire class:

    Java Code:
    package jukebox;
    
    import java.util.ArrayList;
    import java.io.*;
    public class StorageSystem implements Serializable
    {
        ArrayList<Item> itemList = new ArrayList<Item>();   // sets up an array list
    
        private int totalStored;                            // keeps track of the number of stored items
    
        public StorageSystem(int totalStored)               // constructor, gets set to 0
        {
            this.totalStored = totalStored;
        }
    
        public void addItem(Item hold)                      // adds object to the ArrayList
        {
            itemList.add(hold);
    
            totalStored++;
    
    
            try
            {
                FileOutputStream fout = new FileOutputStream("thequeue.ser");
                ObjectOutputStream oos = new ObjectOutputStream(fout);
                oos.writeObject(hold);
                oos.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default

    This may be a stupid question but does your Item class implement the Serializable interface?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Serializing
    By shashanksworld in forum New To Java
    Replies: 7
    Last Post: 03-29-2010, 04:47 PM
  2. serializing pdf files
    By windie in forum New To Java
    Replies: 0
    Last Post: 03-11-2009, 05:46 PM
  3. Object not serializing
    By MamboBanda in forum New To Java
    Replies: 1
    Last Post: 08-12-2008, 12:15 AM
  4. Serializing Arrays
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 10:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •