AvarionDE
AvarionDE

Reputation: 160

Java - NotSerializableException (saving an object)

So today I am working on a plugin for a Minecraft Server. For this, i created a "Track" class which represents a racing track. In order to storage all existing tracks with their data I want save the whole track object which makes everything easier for me. In conclusion, I implemented Serializable to my Track class to use the ObjectOutputStream later. When I start saving the tracks Java creates a file of the object but it gives me a warning/error as well:

14:19:00] [Server thread/WARN]: java.io.NotSerializableException: org.bukkit.craftbukkit.v1_8_R2.CraftWorld [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) [14:19:00] [Server thread/WARN]: at de.avarion.speedrunners.Main.saveTracks(Main.java:206) [14:19:00] [Server thread/WARN]: at de.avarion.speedrunners.Main.onDisable(Main.java:48) [14:19:00] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:323) [14:19:00] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:359) [14:19:00] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:424) [14:19:00] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:417) [14:19:00] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_8_R2.CraftServer.disablePlugins(CraftServer.java:335) [14:19:00] [Server thread/WARN]: at net.minecraft.server.v1_8_R2.MinecraftServer.stop(MinecraftServer.java:458) [14:19:00] [Server thread/WARN]: at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:590) [14:19:00] [Server thread/WARN]: at java.lang.Thread.run(Thread.java:745)

Here is a part of my Track class. I noticed that eclipse is warning me that the track class does not declare a final static serialVersionUID field. I tried to implement the default and generated serialVersionUID but i didn't help:

import java.io.Serializable;
import java.util.ArrayList;

import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;

public class Track implements Serializable {

private int playerSize;

private String name;
private World world;
private ArrayList<Player> players;
private Location spawn;

private Sign sign;
private Material finishBlocks;
private Material speedBlocks;

public Track(World world, String name, int playerSize) {
    this.world = world;
    this.name = name; 
    this.playerSize = playerSize;
    this.players = new ArrayList<Player>();
}
//and so on.....

Now, here is the Code snipet where I save the track object:

private void saveTracks() {
    FileOutputStream outputStream = null;
    ObjectOutputStream objectOutput = null;
    try {
        File directory = new File("tracks");
        if(!directory.exists()) directory.mkdir();
        for(Track track : tracks) {
            outputStream = new FileOutputStream("tracks/" + track.getName() + ".ser");
            objectOutput = new ObjectOutputStream(outputStream);
            objectOutput.writeObject(track);
        }
    }
    catch (IOException e) {
          e.printStackTrace();
    }
    finally {
          if (objectOutput != null) try { objectOutput.close(); } catch (IOException e) {}
          if (outputStream != null) try { outputStream.close(); } catch (IOException e) {}
    }
}

So like i said Java creates the object so it kinda works. But I have no idea how to get rid of the warning.

Thanks for your help

Upvotes: 0

Views: 1336

Answers (1)

Siva Kumar
Siva Kumar

Reputation: 2006

This exception throws because of some instance fields are not serialized. So Please check all the instance variables . If some instance variables are not required to serialize then please add transient to that variables.

As per java doc :

Thrown when an instance is required to have a Serializable interface. The serialization runtime or the class of the instance can throw this exception. The argument should be the name of the class.

Upvotes: 1

Related Questions