Hello good afternoon.

I have successfully serialized data in the past, however it seems as if I don;t fully understand the process. I have a class called ProgramState which stores a variable (called lastCreated) with custom class LinesProject and an ArrayList variable(called recentProjects) of this custom class. The purpose of these variables is to store the last created project as well as a list of five recently opened projects.

Here is ProgramState:

public class ProgramState implements Serializable{

    private LinesProject lastCreated;
    private ArrayList<LinesProject> recentProjects;

    public ProgramState()
    {
        lastCreated=new LinesProject();
        recentProjects=new ArrayList<LinesProject>();
    }
    public ProgramState(LinesProject proj)
    {
        lastCreated=proj;
        recentProjects=new ArrayList<LinesProject>();
    }
    //Setters

    public void setLastCreated(LinesProject project)
    {
        this.lastCreated=project;
    }

    public void setRecentProjects(ArrayList<LinesProject> projects)
    {
        this.recentProjects=projects;
    }
    //Getters
    public LinesProject getLastCreated()
    {
        return this.lastCreated;
    }
    public ArrayList<LinesProject> getRecentProjects()
    {
        return this.recentProjects;
    }
    public LinesProject getProject(int pos)
    {
        return this.recentProjects.get(pos);
    }
    //Adders?
    public void addProject(LinesProject project)
    {
        this.recentProjects.add(project);

    }
    public void addRecentProjects(ArrayList<LinesProject> projects)
    {
        this.recentProjects=projects;

    }

    //Others
    public void addToRecentProjects(LinesProject proj)
    {
        for(int i=0;i<recentProjects.size();i++)
        {
            if(proj.getProjectName().equals(recentProjects.get(i).getProjectName()))
            {
                recentProjects.remove(i);

                recentProjects.add(i, proj);
                break;
            }
            else if(i==(recentProjects.size()-1))
            {
                if(!(this.recentProjects.size()==5))
                {

                    this.recentProjects.add(proj);
                }
                else
                {
                    this.recentProjects.remove(4);

                    this.recentProjects.add(proj);
                }
                break;
            }
        }

    }

    public void registerLastCreated(LinesProject proj)
    {
        proj.iteratePlus();
        this.lastCreated=proj;
        //this.recentProjects.add(proj);
        //this.addToRecentProjects(proj);
        for(int i=0;i<recentProjects.size();i++)
        {
            if(proj.getProjectName().equals(recentProjects.get(i).getProjectName()))
            {
                recentProjects.remove(i);

                recentProjects.add(i, proj);
                break;
            }
            else if(i==(recentProjects.size()-1))
            {
                if(!(this.recentProjects.size()==5))
                {

                    this.recentProjects.add(proj);
                }
                else
                {
                    this.recentProjects.remove(4);

                    this.recentProjects.add(proj);
                }
                break;
            }
        }
    }
}

And here is LinesProject:

public LinesProject()
    {
        fileLocation="";
        projectName="";
        counter=0;
        sync=false;
    }
    public LinesProject(String fl,String fN,boolean state)
    {
        fileLocation=fl;
        projectName=fN;
        sync=state;
    }
    //Setter Methods
    public void setFileLocation(String str)
    {
        this.fileLocation=str;
    }
    public void setProjectName(String str)
    {
        this.projectName=str;
    }
    public void setSyncAutomatically(boolean var)
    {
        this.sync=var;
    }
    public void setSync(boolean val)
    {
        this.sync=val;
    }
    public void setCounter(int val)
    {
        this.counter=val;
    }

    //Getter Methods
    public int getCounter()
    {
        return counter;
    }
    public String getFileLocation()
    {
        return this.fileLocation;
    }
    public String getProjectName()
    {
        return this.projectName;
    }
    public boolean getSync()
    {
        return this.sync;

    }
    public boolean shouldSyncAutomatically()
    {
        return this.sync;
    }
    //Other Methods
    public void iteratePlus()
    {
        counter++;
    }
    public void iterateMinus()
    {
        counter--;
    }

Here is my problem: Before I add my last created project to the arraylist of recent projects I need to do some calculations with the arraylist to ensure that I insert the last created project at the proper location within it. However this seems to prevent the serialization of the arraylist. I am certain that I have met the standards for creating a Javabean for each class. So is there a reason why my program refuses to serialize both of those variables please?

Recommended Answers

All 3 Replies

have you tried making your LinesProject class Serializable?

If you are using Serialization (ie write/readObject) then bean standards are irrelevant, but you must declare every class involved as "implements Serializable" as a way of asserting that your classes don't contain anything the can't be serialized - eg references to live operating system handles.
If you are using javabeans XML serialization then you must conform to java beans standards, and the Serialization marker interface is irrelevant.

Ooops. The problem was actually semantic. I made an error in a loop that I used. :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.