i want to save some of my classes when my java program is running. So i can load them if i start the program again. It works just fine with 2 out of 3 classes (the save() code is the same in all classes except for the saveURL). but this one seems to have some problems.. The wierd thing is, the save() method works the first 2-3 times while running, but then it start to throw errors.. The method calls appear 5-10s apart, so that should not be the problem. What can i do about it? I already set PageLayoutMedia to "implements Serializable"..

@SuppressWarnings("serial")
public class DataManager implements Serializable{

private LinkedList<PageLayoutMedia> mediaPages = new LinkedList<PageLayoutMedia>();

public Datamanager(...){ ... }

    private void privateSave(){
    try{
        //delete old save file
        MyFileWriter.deleteFile(saveURL);
        // Open a file to write to
        FileOutputStream saveFile=new FileOutputStream(saveURL);
        // Create an ObjectOutputStream to put objects into save file.
        ObjectOutputStream save = new ObjectOutputStream(saveFile);

        // Now we do the save.
        save.writeObject(mediaPages);

        // Close the file.
        save.close(); // This also closes saveFile.         
    }
        catch(Exception e){
        // if we land here - we got an error
        }
    }

scenario:

  • 1st save() mediaPages is initialized but empty: save complete
  • 2nd save() mediaPages has one element: save complete
  • 3nd save() mediaPages has two elements: throws an error :(

    @SuppressWarnings("serial")
    public abstract class PageLayout implements Serializable{
    
    private long ID;
    private String URL;
    protected static String backgroundColor;
    protected static Dimension dim;
    protected static String mediaURL;
    
    public PageLayout(Long ID, String URL) {    
        ...
    }   
    }
    
    
    @SuppressWarnings("serial")
    public abstract class PageLayoutMedia extends PageLayout{
    private AnzeigeZeitraum zeitraum;
    protected Dimension mediaDim;
    protected String mediaURL;
    protected int borderTop;
    
    public PageLayoutMedia(Long ID, String URL, AnzeigeZeitraum zeitraum, Dimension mediaDim, String mediaURL) {
    super(ID, URL);
    ...
    }
    }
    
    @SuppressWarnings("serial")
    public class PageLayoutFullpicture extends PageLayoutMedia {
    
    public PageLayoutFullpicture(AnzeigeZeitraum zeitraum, Dimension mediaDim, String mediaURL, Long ID, String URL) {
    super(ID, URL, zeitraum, mediaDim, mediaURL);
    ...
    }
    }
    
有帮助吗?

解决方案

Assumption: At the 3rd save, your PageLayoutMedia objects contain values which are not Serializable.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top