Solved java.io.NotSerializableException

Discussion in 'Plugin Development' started by TheBoy3, Jun 21, 2013.

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

    TheBoy3

    I've recently started development on a plugin that my friend wanted me to make for his server, and I have started making a custom kit system, but when I try making a kit with in-game commands, I get a java.io.NotSerializableException. Here is the necessary code for this:

    Custom Kit code in MultiPlex.java: (Main Plugin Class)

    Code:java
    1.  
    2. public boolean newkitfired_last;
    3.  
    4. public String unikitname;
    5. public Float unikitprice;
    6.  

    at the beginning of the class, and
    Code:java
    1. }
    2. else if (commandLabel.equalsIgnoreCase("newkit")) {
    3. if (args.length == 0 && newkitfired_last) {
    4. ItemStack[] kitcontents = player.getInventory().getContents();
    5. CustomKit newkit = new CustomKit(unikitname, unikitprice, kitcontents);
    6. try {
    7. SLAPI.save(newkit, getConfig().getCurrentPath() + unikitname + ".mpkit");
    8. } catch (Exception e) {
    9. e.printStackTrace();
    10. }
    11. unikitname = null; unikitprice = null; kitcontents = null;
    12. newkitfired_last = false;
    13. } else if (args.length == 2 && !newkitfired_last) {
    14. unikitname = args[0];
    15. unikitprice = Float.parseFloat(args[1]);
    16.  
    17. player.sendMessage(ChatColor.UNDERLINE + "Please empty your inventory, and fill it with only the items you want in this kit. When you are done, type '/newkit' again. (with no arguments)");
    18. newkitfired_last = true;
    19. }
    20. }
    21.  

    In the onCommand method.

    Code in CustomKit.java:

    Code:java
    1.  
    2. package plugin.TheBoy3.MultiPlex;
    3.  
    4. import java.io.Serializable;
    5.  
    6. import org.bukkit.inventory.ItemStack;
    7.  
    8. public class CustomKit implements Serializable {
    9.  
    10. private static final long serialVersionUID = 4821963583737840696L;
    11.  
    12. public String kitname;
    13. public Float kitprice;
    14. public ItemStack[] kitcontents;
    15.  
    16. public CustomKit(String name, Float price, ItemStack[] contents) {
    17. kitname = name;
    18. kitprice = price;
    19. kitcontents = contents;
    20. }
    21.  
    22. public String getName() {
    23. return kitname;
    24. }
    25.  
    26. public Float getPrice() {
    27. return kitprice;
    28. }
    29.  
    30. public ItemStack[] getContents() {
    31. return kitcontents;
    32. }
    33. }
    34.  


    I also implemented Serializable and generated a Serial code for all the classes, (just in case) and I used SLAPI (Saving and Loading API) to save the kit.

    Please help, thanks!
     
  2. Offline

    Rocoty

    The problem is that, when you want to serialize an object, you have to make sure that ALL members of the class are either Serializable or transient (making them transient would tell the serializer not to serialize them). ItemStack is not serializeable, and therefore your objects won't serialize.
     
  3. Offline

    TheBoy3

    I see. How could I fix that problem of the non-serializable ItemStack class?
     
  4. Offline

    LucasEmanuel

    TheBoy3
    That depends on why you want to serialize an itemstack. If you want to save it to a configfile, just use this:
    Code:
    config.set("node", itemstack);
     
  5. Offline

    TheBoy3


    Oh, thanks for the suggestion! I'll try using that code.

    I tried your suggestion, and the saving works great! But now i'm having a problem loading the config node. Here is the stack trace i'm getting:

    Code:
    20:00:19 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'getkit' in plugin MultiPlex v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:971)
        at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:889)
        at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:846)
        at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
        at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:115)
        at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
        at plugin.TheBoy3.MultiPlex.MultiPlex.onCommand(MultiPlex.java:141)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    and here is the code of the /getkit command:

    Code:java
    1.  
    2. if (args.length == 1) {
    3. CustomKit retrievedkit = (CustomKit) config.get("kit." + args[0]);
    4. for(ItemStack is : retrievedkit.kitcontents) {
    5. if (is != null) {
    6. player.getInventory().addItem(is);
    7. }
    8. }
    9. }
    10.  


    How can I fix this?

    Nevermind, it's working perfectly! Thanks Rocoty and LucasEmanuel for helping me! :)

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

Share This Page