2

I am trying to serialize this arraylist:

static ArrayList<Product> Chart=new ArrayList<Product>();

with these objects:

double Total;
String name;
double quantity;
String unit;
double ProductPrice

This is the class so far:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Product implements Serializable{
double Total;
String name;
double quantity;
String unit;
double ProductPrice;

public Product(String n)
{
    name=n;
}
private void writeObject(ObjectOutputStream s) throws IOException
{
    s.defaultWriteObject();
    Product pt=new Product(name);
    ObjectOutputStream oos=new ObjectOutputStream(s);
    oos.writeObject(pt);
}
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
{
    s.defaultReadObject();
    Product pt;
    ObjectInputStream ios =new ObjectInputStream(s);
    ObjectInputStream ois = null;
    pt=(Product)ois.readObject();
}


}

I am trying to serialize and deserialize the arraylist(declared in another class) so that the objects in the arraylist will be saved between runtimes. Any ideas?

Vincent Taglia
  • 173
  • 1
  • 3
  • 12
  • 3
    `ArrayList` implements `Serializable`... whats the issue? – Paul Bellora Aug 06 '12 at 21:41
  • Don't make things harder if they can be simplier [how to serialize a list to xml using Xstream library][1] [1]: http://stackoverflow.com/questions/10051501/xstream-how-to-serialize-a-list-to-xml – dantuch Aug 06 '12 at 21:44

3 Answers3

5

Why are you creating new Product objects in these methods? They are not static, so I would assume they should operate on this? You also are trying to call readObject() on an Object you just set to null.

If you can give some more details about the errors you're seeing and how you're using this, we can probably help more.

Edit: Added some sample code

Write it out:

    Product p = new Product("My Product");
    try
    {
       FileOutputStream fileOut =
       new FileOutputStream("product.ser");
       ObjectOutputStream out = new ObjectOutputStream(fileOut);
       out.writeObject(p);
       out.close();
       fileOut.close();
    } catch(IOException ioe)
    {
        ioe.printStackTrace();
    }

Read it in:

    Product p = null;
    try
    {
        FileInputStream fileIn = new FileInputStream("product.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        p = (Product) in.readObject();
        in.close();
        fileIn.close();
    } catch(IOException ioe)
    {
        ioe.printStackTrace();
        return;
    } catch(ClassNotFoundException c)
    {
        System.out.println(.Product class not found.);
        c.printStackTrace();
        return;
    }
Carl
  • 905
  • 5
  • 9
  • I don't have any errors I just don't know where to go from here. This is the first time I have ever worked with serializing anything – Vincent Taglia Aug 07 '12 at 02:33
  • Ok, I have updated the post to show some sample code adapted for your class. You can remove the read and write methods from the class, and use this code in `main` (or wherever) to write or read the object in use. – Carl Aug 07 '12 at 04:36
1

It doesn't look like there is any need for Product to provide readObject and writeObject methods. You should just be able to serialise and deserialise the List as is.

I'd suggest wrapping the list in a class that makes sense in the context. (I don't know what the context is, or what the order is (would a Set be better).) Also mutable statics are generally a bad idea, in particular if you are going to try to serialise and deserialise the referenced object.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

The ArrayList class already implements Serializable, and you made your class (Product) serializable; everything seems write to me. "so that the objects in the arraylist will be saved between runtimes." You make it sound like you think it should automatically save them between each time you run it; this might be your mistake. You must write it to a file, and read it the next execution (Use ObjectOutput(/Input)Streams)

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31