Sir Code-A-Lot
Sir Code-A-Lot

Reputation: 75

Serializing an Arraylist<CustomObject>

I have created an Object called "Item", and I want to serialize an ArrayList with Items inside it. My program works perfectly with an ArrayList<String>, but it doesn't work with an ArrayList<Item>. I believe it has to do with my Object. Here it is:

public class Item implements Serializable{

private static String name;
private static BufferedImage picture;
private static boolean craftable;
private static Item[][] craftTable;
private static boolean smeltable;
private static Item smelt_ancestor;
private static Item smelt_descendant;

public Item(String name, boolean craftable, boolean smeltable){
    this.name = name;
    this.craftable = craftable;
    if(craftable){
        craftTable = new Item[3][3];
    }else{
        craftTable = null;
    }
    this.picture = null;
    this.smeltable = smeltable;
    this.smelt_ancestor = null;
    this.smelt_descendant = null;
}

public String getName(){
    return name;
}

public void setName(String name){
    this.name=name;
}

public BufferedImage getPicture(){
    return picture;
}

public boolean setPicture(){
    boolean verify = false;
    String pictureName = name.replaceAll("\\s+","");
    String newNamePng = pictureName + ".png";
    String newNameJpg = pictureName + ".jpg";
    File imagePng = new File(newNamePng);
    File imageJpg = new File(newNameJpg);
    if(imagePng.exists()){
        return true;
    }else if(imageJpg.exists()){
        return true;
    }else{
        return false;
    }
}

public boolean getCraftable(){
    return craftable;
}

public void setCraftable(boolean value){

    this.craftable = value;
}

public boolean setCraftTable(Item[][] table){
    if(this.craftable==true){
        craftTable = table;
        return true;
    }else{
        return false;
    }

}

public Item[][] getCraftTable(){
    return craftTable;
}

public boolean getSmeltable(){
    return smeltable;
}

public void setSmeltable(boolean value){
    smeltable = value;
}

public Item getAncestor(){
    return smelt_ancestor;
}

public void setAncestor(Item ancestor){
    smelt_ancestor = ancestor;
}

public Item getDescendant(){
    return smelt_descendant;
}

public void setDescendant(Item des){
    smelt_descendant = des;
}

public String toString(){
    return name;
}

Ignore the imports, I use them in other methods I omitted because they work perfectly. Is there anything wrong with the Object that could stop it from being serialized correctly?

Upvotes: 0

Views: 48

Answers (2)

Waqas Memon
Waqas Memon

Reputation: 1249

Serialization by definition is applied on objects and not classes. It copies the state of an object to be transferred over a network or a stream or to be stored. Your usage of static variables make them class variables, and hence they do not contribute to the state of an object. First thing to do would be make them non-static.

This leaves us with your class, which is already Serializable and so is ArrayList. You can just serialize them and de-serialize them using ObjectInputStream and ObjectOutputStream and by making sure you have the same class in the classpath at the Deserialization end.

Upvotes: 0

Chris Wilson
Chris Wilson

Reputation: 46

Static variables are not serialized. It looks like you probably want those to be non-static instance variables.

Upvotes: 2

Related Questions