[SOLVED] Serialization Errors

Discussion in 'Plugin Development' started by Malikk, Aug 10, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Malikk

    Hey Everybody,

    Just wondering how most of you go about persisting objects. I've been serializing things since my first plugin, but it's kinda a huge pain... lol.
     
  2. What's the pain about serialization? Anyway, the only other solution I see is flatfile/yml/sql and then re-creating the objects with the saved information. But serialization is way less pain, isn't it? ;)
     
  3. Offline

    Malikk

    It's usually not a huge pain, but it's giving me a lot of problems right now.

    It keeps throwing notSerializableExceptions, but on totally random classes that it shouldn't even be using. Just to appease it, I tried making these classes implement serialize as well, but one after another, it just kept throwing different errors.

    Are there just certain things that can't be serialized?

    Here's the class I'm trying to serialize
    Flag class (open)

    Code:
    /*
    * Copyright 2012 Jordan Hobgood
    *
    * This file is part of Shield.
    *
    * Shield is free software: you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation, either version 3 of the License, or
    * (at your option) any later version.
    *
    * Shield is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    * GNU General Public License for more details.
    * 
    * You should have received a copy of the GNU General Public License
    * along with Shield.  If not, see <http://www.gnu.org/licenses/>.
    */
     
    package com.malikk.shield.flags;
     
    import java.io.Serializable;
    import java.util.HashSet;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
     
    import com.malikk.shield.regions.ShieldRegion;
     
    /**
    * A flag object allows for sets of players to be assigned boolean values in certain regions.
    * @author Malikk
    * @see {@link #getName()}
    * @see {@link #getRegion()}
    * @see {@link #getPlayers()}
    * @see {@link #getValue()}
    * @see {@link #setPlayers(players)}
    * @see {@link #setValue(boolean)}
    * @see {@link #addPlayer(player)}
    * @see {@link #addPlayers(players)}
    * @see {@link #removePlayer(player)}
    * @see {@link #removePlayers(players)}
    */
    public class Flag implements Serializable{
     
        private static final long serialVersionUID = -8016185180582202784L;
       
        private String name;
        private ShieldRegion region;
        private HashSet<String> players;
        private boolean value;
     
        public Flag(String flag, ShieldRegion region, HashSet<String> players, boolean value){
            this.name = flag;
            this.region = region;
            this.players = players;
            this.value = value;
        }
       
        /**
        * Gets the name of the flag.
        *
        * @return String - name
        */
        public String getName(){
            return name;
        }
       
        /**
        * Gets the ShieldRegion the flag is set at.
        *
        * @return {@link ShieldRegion} that the flag is set on
        */
        public ShieldRegion getRegion(){
            return region;
        }
       
        /**
        * Gets all the Players that the flag is assigned to
        *
        * @param players - HashSet<{@linkplain Player}>
        */
        public HashSet<Player> getPlayers(){
            HashSet<Player> set = new HashSet<Player>();
           
            for (String p: getPlayerNames()){
                set.add(Bukkit.getPlayer(p));
            }
           
            return set;
        }
       
        /**
        * Gets all the Player's names that the flag is assigned to
        *
        * @param players - HashSet<{@linkplain String}>
        */
        public HashSet<String> getPlayerNames(){
            return players;
        }
       
        /**
        * Gets the value that the flag will return for players in its HashSet. For players not in the HashSet, the opposite of this value will be returned.
        *
        * @return value - {@link Boolean} assigned to the flag
        */
        public boolean getValue(){
            return value;
        }
       
        /**
        * Sets the HashSet of player names for this flag to the one passed in. (Will completely overwrite the previous Set)
        *
        * @param players - HashSet<{@linkplain String}>
        */
        public void setPlayers(HashSet<String> players){
            this.players = players;
        }
       
        /**
        * Sets the value of the flag.
        *
        * @param value - {@link Boolean}
        */
        public void setValue(boolean value){
            this.value = value;
        }
       
        /**
        * Adds a player to the flag's current HashSet.
        *
        * @param player - {@link Player}
        */
        public void addPlayer(Player player){
            this.players.add(player.getName());
        }
       
        /**
        * Adds players to the flag's current HashSet.
        *
        * @param players - HashSet<{@linkplain String}>
        */
        public void addPlayers(HashSet<String> players){
            for (String p: players){
                this.players.add(p);
            }
        }
       
        /**
        * Removes a player from the flag's current Set.
        *
        * @param player - {@link Player}
        */
        public void removePlayer(Player player){
            this.players.remove(player.getName());
        }
       
        /**
        * Removes players from the flag's current Set.
        *
        * @param players - HashSet<{@linkplain String}>
        */
        public void removePlayers(HashSet<String> players){
            for (String p: players){
                this.players.remove(p);
            }
        }
    }
    


    The error log is little to no help. All is says is that my main class isn't serializable, but it shouldn't need to be. I'm quite confused here.

    Error log (open)

    Code:
    05:00:44 [SEVERE] java.io.NotSerializableException: com.malikk.shield.Shield
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
    05:00:44 [SEVERE]    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    05:00:44 [SEVERE]    at com.malikk.shield.flags.FlagPersister.save(FlagPersister.java:66)
    05:00:44 [SEVERE]    at com.malikk.shield.Shield.onDisable(Shield.java:83)
    05:00:44 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
    05:00:44 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:383)
    05:00:44 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:400)
    05:00:44 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:393)
    05:00:44 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:434)
    05:00:44 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:534)
    05:00:44 [SEVERE]    at org.bukkit.Bukkit.reload(Bukkit.java:182)
    05:00:44 [SEVERE]    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:22)
    05:00:44 [SEVERE]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
    05:00:44 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:490)
    05:00:44 [SEVERE]    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:862)
    05:00:44 [SEVERE]    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:822)
    05:00:44 [SEVERE]    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:804)
    05:00:44 [SEVERE]    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    05:00:44 [SEVERE]    at net.minecraft.server.NetworkManager.b(NetworkManager.java:281)
    05:00:44 [SEVERE]    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:105)
    05:00:44 [SEVERE]    at net.minecraft.server.ServerConnection.b(SourceFile:35)
    05:00:44 [SEVERE]    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    05:00:44 [SEVERE]    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:559)
    05:00:44 [SEVERE]    at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
    05:00:44 [SEVERE]    at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    05:00:44 [SEVERE]    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    05:00:44 [SEVERE]    at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    


    I suppose the problem is the other data object of mine within this one.

    Ignore all that crap... It's late-- err, early. lol

    Added all the objects I needed to recreate my own data object, and serialized it all together, rather than as one of my objects inside another, and it all works great. Not sure why I had such a stupidly hard time figuring that out.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
Thread Status:
Not open for further replies.

Share This Page