0

I'd Like to serialize a list, When it saves it does contain information that one of the elements in the list is Child but when i try to read it it returns a not initialized parent class

public static List<Parent> myList = new ArrayList<Parent>();
ds = new Child(3, 4, 3, "zh", "hj" , "ni", p);
myList.add(ds);

try {
        logToFile(myList, saveTo);                 //My method for saving
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

myList = (List<Parent>) readFromFile(saveTo); //My method for reading
//My list containing null for each Child

i should probably also include that Parent is abstract class and extends 3rd party class

Edit: here are my save and read functions

    public void logToFile(Object o, File f) throws URISyntaxException

    {

        try
        {
            FileOutputStream fout = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fout);
            oos.writeObject(o);
            oos.close();

        } catch (IOException e)
        {

            e.printStackTrace();

        }

    }


        public Object readFromFile(File f) throws ClassNotFoundException, IOException {

        FileInputStream is = new FileInputStream(f);
        ObjectInputStream g = new ObjectInputStream(is);

        Object oggg = g.readObject();

        g.close();
        return oggg;


    }

Parent's and child's class

 public class Child extends Parent implements java.io.Serializable{


/**
 * 
 */
private static final long serialVersionUID = -8270915411149374357L;

public Child(int x, int z, int y, Material m, String Title) {
    super(m, Title);
    this.x = x;
    this.y = y;
    this.z = z;
}


public abstract class Parent extends ItemStack implements java.io.Serializable{

//ItemStack is a 3rd party class

/**
 * 
 */
private static final long serialVersionUID = -904093000933705904L;

public Parent(Material M, String name) {
    super(M);
    setName(name);
}

Short fragment of a text file produced by program:

Short fragment of a text file produced by program

Vordex
  • 52
  • 1
  • 10
  • With java conventions names should be myList, not MyList. I suppose it would be better if you explain serialization mechanism, which you use. – egorlitvinenko Sep 09 '17 at 08:47
  • 2
    So, how is Child defined? How is Parent defined? How do you observe the content of what you read? Post a complete minimal example reproducing the problem. You're almost there anyway. – JB Nizet Sep 09 '17 at 09:02

0 Answers0