Hi all,

I'm trying to write a program that holds student data in arraylists. I've managed to get all the adding, editing, deleting, displaying etc. working and now all I need to do is to make sure it saves on exit and loads on startup

By following another tutorial, I have managed to get the data saved into a file, but I can't get it to load back in again. I know there is something missing and I'm pretty sure it's obvious, but I'm having a total 'blonde moment' and can't think what it is!

Any help is appreciated, if you need more code, just say

Jamie


Save code:

private static void doSave() {
		
		try{
			FileOutputStream fos = new FileOutputStream("firstyears.dat");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(FirstYears);
			oos.close();
			fos.close();
		}
		catch (FileNotFoundException e){
			
			e.printStackTrace();
		
		}
		catch (IOException e){
			
			e.printStackTrace();
			
		}
	}

Load code:

private static void doLoad() {

		try{
			
			FileInputStream fis = new FileInputStream("firstyears.dat");
			ObjectInputStream ois = new ObjectInputStream(fis);
			Object obj = ois.readObject();
			ArrayList<FirstYearStudent> FirstYears = (ArrayList<FirstYearStudent>) obj;
		}
		catch (FileNotFoundException e){
			
			e.printStackTrace();
		}
		catch (IOException e){
			
			e.printStackTrace();
		}
		catch (ClassNotFoundException e){
			
			e.printStackTrace();
		}
		
	}

FirstYearStudent class:

package napier.coursework.sd3;

import java.io.Serializable;

public class FirstYearStudent implements Serializable{

	private static final long serialVersionUID = 1L;
	private String fname = null;
	private String lname = null;
	private String eats = null;
	private String location = null;
	

	public FirstYearStudent(String fname, String lname, String eats,
			String location) {

		this.setFname(fname);
		this.lname = lname;
		this.eats = eats;
		this.location = location;
		
	}
	
	public String toString() {
	return getFname() + " " + lname + " - " + eats + " - " + location;
	}

	public void setFname(String fname) {
		this.fname = fname;
	}

	public String getFname() {
		return fname;
	}

	public void setLname(String lname) {
		this.lname = lname;
	}

	public String getLname() {
		return lname;
	}
	
	public void setEats(String eats) {
		this.eats = eats;
	}

	public String getEats() {
		return eats;
	}
	
	public void setLocation(String location) {
		this.location = location;
	}

	public String getLocation() {
		return location;
	}
}

Your load code looks OK to me at a quick scan, but there's a big problem with what you do next. You create a new local variable FirstYears (NB should be firstYears), read the file into it, then end the method - at which point FirstYears goes out of scope and the object is garbage collected.
You need to either assign the newly-read object to a field of the class or (probably better) return it as a return value from your method.

Aha, fixed it. Thanks, that was just the memory jog I needed

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.