Vincent Taglia
Vincent Taglia

Reputation: 173

How to serialize an arraylist

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?

Upvotes: 2

Views: 5261

Answers (3)

Carl
Carl

Reputation: 915

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;
    }

Upvotes: 5

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

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.

Upvotes: 1

Alex Coleman
Alex Coleman

Reputation: 7326

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)

Upvotes: 0

Related Questions