I have the following class:
public class GameWorld implements Serializable {
int sumu_dist = 30;
public List<Tiles> tileGroup = new ArrayList<>();
public List<Tiles> loadingTiles = new ArrayList<>();
// constructor etc.
}
in another class, I tried to save GameWorld
, but received not serializable exception:
public class game {
static GameWorld GameWorldObj = new GameWorld();
String FileName = "WorldData.bin";
void Save(){
try {
FileOutputStream FOS = new FileOutputStream(FileName);
ObjectOutputStream OOS = new ObjectOutputStream(FOS);
if (gameworld_obj instanceof Serializable){
OOS.writeObject(GameWorldObj); // java.io.NotSerializableException
}
System.out.printf("SAVED: %s \n", OOS);
OOS.close();
} catch (IOException exception){
System.err.println(String.valueOf(exception));
}
}
public static void main(String[] args) {
new game().Save();
}
I don't know why it's happen, I search on SO and find some answers told you to implement Serializable
. I did it, and I'm still receiving NotSerializableException
.
Why is that to happen? What can I do to fix it?
Error-message:
java.io.NotSerializableException: source.library.level.Tiles
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1197)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1582)
at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1539)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1448)
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1191)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354)
at source.library.level.game.Save(game.java:33)
at source.library.level.game.main(game.java:47)