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?
Why are you creating new
Productobjects in these methods? They are not static, so I would assume they should operate onthis? You also are trying to callreadObject()on an Object you just set tonull.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:
Read it in: