Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alejandro Crespo
6,628 PointsProblems serializing/de-serializing a class that uses a Treemap.
I'm working on a project that is basically just an exercise logbook, its fairly straightforward. You input the "exercises" done on a particular "workout", the "workout" itself is a list of the exercises and the date in which they were done. Then, the "workout" is added into the "workoutlog", which is just a class that contains a Treemap, that uses the date of the workout as the key and the "workout" as the value. Finally, the "workoutlog", is serialized and voila you have a record of your workouts.
The problem is that when de-serializing I'm getting a class cast exception and thus cannot review all the workouts added to the workoutlog. Now, I know its serializing correctly because I checked the file where the workouts should be being written to and they are all there. The problem seems to be when de-serializing something causes the class cast exception. Any insight would be super appreciated, this is starting to give me a headache haha.
Here are the relevant lines of code:
This is the workoutlog class:
/*
This class is the actual log book that will be stored. It is a treemap, so it will be organized by key values.
The key will take a String value that will be the formatted date from the workout date member variable.
*/
public class WorkoutLog implements Serializable{
public TreeMap < String , Workout > mWorkoutLog;
// this is the actual Workoutlog
public WorkoutLog(){
mWorkoutLog = new TreeMap<>();
}
public TreeMap < String, Workout> getWorkoutLog(){
return mWorkoutLog;
}
}
This is where the error is occurring:
case Logger.CHECK_KEY:
try {
workoutLog = (WorkoutLog) SerializationUtil.deserialize(file);
System.out.println("Deserializing from:..." + file.getName());
System.out.println(workoutLog.getWorkoutLog().keySet()+"");
} catch(EOFException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
[MOD: added ```java formatting -cf]