Hashtable problem - java.io.NotSerializableException

I'm having trouble writing a hashtable out to a file. I know that the writeObject() method in ObjectOutputStream requires whatever it writes to be serializable, so I made the PlayerScore class implement serializable.. Now it gives "Not serializable Exeption: java.io.NotSerializableException: PluginMain" where PluginMain is the class that contains the hashtable. If I have it Implement Serializable I get the same error with what I would guess is a super class of mine named BotCore.

All the source is here: www.witcheshovel.com/WartsStuff/PluginMain.java

Here are what I believe to be the offending portions of the code:

private Hashtable<String, PlayerScore> scoreTable;

private void saveScores() {

try {

out.showMessage("Hash a a string: " + scoreTable.toString());

String scoreFile = getSetting("8. Score File");

FileOutputStream fileOut = new FileOutputStream (scoreFile);

ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);

Object scores = (Object)scoreTable;

//objectOut.writeObject(scores);

objectOut.writeObject(scoreTable);

objectOut.close();

fileOut.close();

}

catch (NotSerializableException a) {out.showMessage("Not serializable Exeption: " + a);}

catch (Exception e) {out.showMessage("Failed to save file with error:" + e);}

}

public class PlayerScore implements Serializable {//Class for keeping player scores

String playerName;

int longestStreak=0;

int totalCorrect=0;

public PlayerScore() {}

public PlayerScore(String name) {

playerName = name;

}

public void setTotalCorrect(int newCorrect) {

totalCorrect = newCorrect;

}

public void incTotalCorrect() {

totalCorrect++;

}

public void setLongestStreak(int streak) {

longestStreak=streak;

}

public void incLongestStreak() {

longestStreak++;

}

public String getName() {

return playerName;

}

public int getStreak() {

return longestStreak;

}

public int getTotalCorrect() {

return totalCorrect;

}

}

[2175 byte] By [Warta] at [2007-10-3 5:22:10]
# 1

Does it look like this:

class PluginMain

{

class PlayerScore implements Serializable

{

}

}

If PlayerScore (or anything else you try to serialize) is an inner class it has a hidden reference to its containing class (i.e. PluginMain.this), so serializing it will try to serialize the containing class. You can make the nested class static, or place it outside the scope of the containing class. If it still needs a reference to the containing class put it in explicitly and make it transient, but you will have a problem at the receiving end.

ejpa at 2007-7-14 23:29:11 > top of Java-index,Core,Core APIs...
# 2
Making the PlayerScore class static worked :) It was nested inside. Thank you thank you thank you :)
Warta at 2007-7-14 23:29:11 > top of Java-index,Core,Core APIs...