Arraylist of custom objects serialization

I have two array lists in my code. One stores User objects, the other stores Team objects. What I'm trying to do is when the close instruction is given save both array lists to files, then close the program.

I was having problems saving both of the AL's, but then I defined an serialVersionUID and only the User AL us working fine. I still get an error from the Team AL.

Error saving:

14:29:46 [SEVERE] java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: org.bukkit.Location
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject(Unknown Source)
14:29:46 [SEVERE]       at java.util.ArrayList.readObject(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.lang.reflect.Method.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject(Unknown Source)
14:29:46 [SEVERE]       at plugin.Configs.loadTeams(Configs.java:129)
14:29:46 [SEVERE]       at plugin.UltimateSurvival.onEnable(UltimateSurvival.java:82)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.java:284)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.java:266)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:615)
14:29:46 [SEVERE]       at org.bukkit.Bukkit.reload(Bukkit.java:277)
14:29:46 [SEVERE]       at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
14:29:46 [SEVERE]       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:968)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:886)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:837)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
14:29:46 [SEVERE] Caused by: java.io.NotSerializableException: org.bukkit.Location
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source)
14:29:46 [SEVERE]       at java.util.ArrayList.writeObject(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.lang.reflect.Method.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source)
14:29:46 [SEVERE]       at plugin.Configs.saveTeams(Configs.java:52)
14:29:46 [SEVERE]       at plugin.UltimateSurvival.onDisable(UltimateSurvival.java:91)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:400)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:393)
14:29:46 [SEVERE]       at     org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:434)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:585)
14:29:46 [SEVERE]       ... 17 more

Error loading:

14:29:46 [SEVERE] java.io.NotSerializableException: org.bukkit.Location
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source)
14:29:46 [SEVERE]       at java.util.ArrayList.writeObject(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.lang.reflect.Method.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source)
14:29:46 [SEVERE]       at plugin.Configs.saveTeams(Configs.java:52)
14:29:46 [SEVERE]       at plugin.UltimateSurvival.onDisable(UltimateSurvival.java:91)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:400)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:393)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:434)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:585)
14:29:46 [SEVERE]       at org.bukkit.Bukkit.reload(Bukkit.java:277)
14:29:46 [SEVERE]       at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
14:29:46 [SEVERE]       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:96
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:886)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:837)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)

This is a part of a minecraft plugin I am currently developing. Some code:

public class Team implements Serializable
{
private static final long serialVersionUID = 6482400621090209853L;
private  String name = "";
private  int points = 0;
private  int size = 0;
private Location spawn = null;
}

public void loadTeams()
{
    try{
        FileInputStream file = new FileInputStream ("plugins/UltimateSurvival/teams.bin");
        ObjectInputStream ois = new ObjectInputStream(file);
        UltimateSurvival.teams = (ArrayList<Team>)ois.readObject();
        file.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public void saveTeams()
{
    try
    {
        FileOutputStream file = new FileOutputStream ("plugins/UltimateSurvival/teams.bin");
        ObjectOutputStream oos = new ObjectOutputStream(file);
        oos.writeObject(UltimateSurvival.teams);
        file.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

I have read a bit about serialization, but it is my first contact with it. Am I doing something wrong?

Thanks in advance

Answers


Your problem is the Location is not serializable:

java.io.NotSerializableException: org.bukkit.Location

To make it serializable, first convert it to a string: worldName,x,y,z, which you could do using this:

public String locationToString(Location l){
    String world = l.getWorld().getName(); //get the world name
    Double x = l.getX(); //get the x coordinate
    Double y = l.getY(); //get the y coordinate
    Double z = l.getZ(); //get the z coordinate

    return world + "," + x + "," + y + "," + z; //return the location, but in string form
}

Then, if you wanted to get the location from the string, you could do:

public Location stringToLocation(String s){
    String[] str = s.split(","); //split s by ','
    World world = Bukkit.getWorld(str[0]); //get the world
    Double x = Double.parseDouble(str[1]); //get the x coord
    Double y = Double.parseDouble(str[2]); //get the y coord
    Double z = Double.parseDouble(str[3]); //get the z coord

    return new Location(world, x, y, z); //return a location created from the String
}

By doing this, you have now converted a Location to a String, using locationToString() which you can put in an Array, then, you can take the String out of the Array, and convert it to a Location using stringToLocation().


I have just

package tv.usasvideos.survivalgameteams;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SaveLoadList {
    public static void save(Object obj,String path) throws Exception {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))) {
            oos.writeObject(obj);
            oos.flush();
        }
    }

    public static Object load(String path) throws Exception {
        Object result;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))) {
            result = ois.readObject();
        }
        return result;
    }
}

and

private List<ChestPoint> chestpoints = new ArrayList<>();

@Override
public void onEnable(){
    try {
        chestpoints = (List<ChestPoint>)SaveLoadList.load("chestpoints.bin");
    } catch (Exception ex) {
        Logger.getLogger(SurvivalGame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void onDisable(){
    try {
        SaveLoadList.save(chestpoints,"chestpoints.bin");
    } catch (Exception ex) {
        Logger.getLogger(SurvivalGame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Need Your Help

Average of row in MySql table

c# mysql sql

I have ordinary sql table in MySql database. I have approximately 150 columns and I would like to compute average of rows of this table. I am going to do this in c#.