Link to home
Start Free TrialLog in
Avatar of k1ngp1n99
k1ngp1n99

asked on

problem saving test data to a file and then reloading it

I am having problems saving data from my HolidayPlannerTester program to file and then reloading it. The program compiles fine but when running the exception message is shown 'save failed'.

Here is my code please post if other code is required.

import java.io.*;

class ObjectFileUtility {

/* Writes an object to the named file. */
public static void saveObject(Serializable obj, String filename) throws IOException

{
ObjectOutputStream oos =
new ObjectOutputStream( new FileOutputStream(filename) );

oos.writeObject(obj);
oos.flush();
oos.close();
}
public static Object loadObject(String filename) throws IOException
{
ObjectInputStream ois =
new ObjectInputStream(
new FileInputStream(filename)
);

try
{
Object obj = ois.readObject();
ois.close();
return obj;
}
catch (ClassNotFoundException e) {
throw new IOException("ClassNotFoundException");
}
}
}

\/\/\/\/\/\/\/\/\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
import java.util.*;

class HolidayPlanner implements java.io.Serializable {
private String companyId;
private HashMap holidayRecords;

public HolidayPlanner(String companyId)
{
this.companyId =  companyId;
holidayRecords = new HashMap();
}

public void addHolidayRecord( HolidayRecord h)
{
String emp = h.getEmployeeID();
holidayRecords.put(emp,h);
}

public void removeHolidayRecord(String employeeID)
{

holidayRecords.remove(employeeID);
}

public HolidayRecord getHolidayRecord(String employeeID)
{
return (HolidayRecord)holidayRecords.get(employeeID);
}

public String toString()

{
return "  Collection for   " +  companyId + " :\n  " + holidayRecords;
}
}

\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
import java.util.*;
class HolidayPlannerTester
{
public static void main(String [] args)
{
HolidayPlanner p = new HolidayPlanner("8788");
HolidayRecord h1 = new HolidayRecord ("1",14);
p.addHolidayRecord(h1);
HolidayRecord h2 = new HolidayRecord ("2",14);
p.addHolidayRecord(h2);
HolidayRecord h3 = new HolidayRecord ("3",7);
p.addHolidayRecord(h3);
HolidayRecord h4 = new HolidayRecord ("4",10);
p.addHolidayRecord(h4);
HolidayRecord h5 = new HolidayRecord ("5",23);
p.addHolidayRecord(h5);
HolidayRecord h6 = new HolidayRecord ("6",7);
p.addHolidayRecord(h6);
HolidayRecord h7 = new HolidayRecord ("7",21);
p.addHolidayRecord(h7);
HolidayRecord h8 = new HolidayRecord ("8",23);
p.addHolidayRecord(h8);
System.out.println(p);
try {
ObjectFileUtility.saveObject(p, "HolidayPlanner.object");
System.out.println("Planner saved.");
}
catch (java.io.IOException e) {
System.out.println("Save failed. " + e.getMessage());
}
}
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>but when running the exception message is shown 'save failed'.

An the rest of the message is..?
Avatar of jimmack
jimmack

Change the e.getMessage() to e.toString() and you'll see the following:

Save failed. java.io.NotSerializableException: HolidayRecord

You need to make HolidayRecord implement the Serializable interface.
ASKER CERTIFIED SOLUTION
Avatar of jimmack
jimmack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of k1ngp1n99

ASKER

Yep that worked thanks for the help again Jimmack ;)

Yep that worked thanks for the help again Jimmack ;)

;-)

No problem.