Q:

Why can this object not be serialized?

Ok, for my Bukkit Plugin, I need the following java class (NewItem.java) to be serializable, but it is not. It gives me the error when my plugin tries to save it:

16:33:23 [SEVERE] java.io.NotSerializableException: org.bukkit.inventory.ItemSta
ck
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:33:23 [SEVERE]       at java.util.ArrayList.writeObject(Unknown Source)
16:33:23 [SEVERE]       at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown
Source)
16:33:23 [SEVERE]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
wn Source)
16:33:23 [SEVERE]       at java.lang.reflect.Method.invoke(Unknown Source)
16:33:23 [SEVERE]       at java.io.ObjectStreamClass.invokeWriteObject(Unknown S
ource)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown So
urce)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknow
n Source)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:33:23 [SEVERE]       at java.util.HashMap.writeObject(Unknown Source)
16:33:23 [SEVERE]       at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown
Source)
16:33:23 [SEVERE]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
wn Source)
16:33:23 [SEVERE]       at java.lang.reflect.Method.invoke(Unknown Source)
16:33:23 [SEVERE]       at java.io.ObjectStreamClass.invokeWriteObject(Unknown S
ource)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown So
urce)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknow
n Source)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Sourc
e)
16:33:23 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source
)
16:33:23 [SEVERE]       at com.tommy3244.plugins.MakeYourOwnBlocks.SaveLoadAPI.s
ave(SaveLoadAPI.java:16)
16:33:23 [SEVERE]       at com.tommy3244.plugins.MakeYourOwnBlocks.MakeYourOwnBl
ocks.onDisable(MakeYourOwnBlocks.java:60)
16:33:23 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
in.java:219)
16:33:23 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin
(JavaPluginLoader.java:481)
16:33:23 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugin(S
implePluginManager.java:400)
16:33:23 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugins(
SimplePluginManager.java:393)
16:33:23 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.clearPlugins(Si
mplePluginManager.java:434)
16:33:23 [SEVERE]       at org.bukkit.craftbukkit.v1_4_6.CraftServer.reload(Craf
tServer.java:563)
16:33:23 [SEVERE]       at org.bukkit.Bukkit.reload(Bukkit.java:184)
16:33:23 [SEVERE]       at org.bukkit.command.defaults.ReloadCommand.execute(Rel
oadCommand.java:23)
16:33:23 [SEVERE]       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
mmandMap.java:186)
16:33:23 [SEVERE]       at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCom
mand(CraftServer.java:514)
16:33:23 [SEVERE]       at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchSer
verCommand(CraftServer.java:506)
16:33:23 [SEVERE]       at net.minecraft.server.v1_4_6.DedicatedServer.al(Dedica
tedServer.java:260)
16:33:23 [SEVERE]       at net.minecraft.server.v1_4_6.DedicatedServer.r(Dedicat
edServer.java:225)
16:33:23 [SEVERE]       at net.minecraft.server.v1_4_6.MinecraftServer.q(Minecra
ftServer.java:494)
16:33:23 [SEVERE]       at net.minecraft.server.v1_4_6.MinecraftServer.run(Minec
raftServer.java:427)
16:33:23 [SEVERE]       at net.minecraft.server.v1_4_6.ThreadServerApplication.r
un(SourceFile:849)

The source code for the NewItem.java is here:

package com.tommy3244.plugins.MakeYourOwnBlocks;import java.util.*;import java.util.logging.Logger;import java.io.Serializable;import org.bukkit.Bukkit;import org.bukkit.Material;import org.bukkit.block.Block;import org.bukkit.configuration.ConfigurationSection;import org.bukkit.entity.Player;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.ShapedRecipe;public class NewItem implements Serializable{   public int id; public byte data;  public int damage; public String name;    public List<String> lore;    public List<List<String>> commands;    public NewItem(List<String> lore, String name, int itemid, byte itemdata, List<List<String>> lines, int damage, boolean hasRecipe, ConfigurationSection section) {      id = itemid;       data = itemdata;       this.name = name;      this.damage = damage;      this.lore = lore;      commands = lines;      ItemStack stack;       if(data != 0)      {          stack = new ItemStack(id, data);       }      else       {          stack = new ItemStack(id);     }      if(hasRecipe)      {      try        {      ShapedRecipe recipe = new ShapedRecipe(stack);     String r1 = section.getString("items."+name+".r1");        String r2 = section.getString("items."+name+".r2");        String r3 = section.getString("items."+name+".r3");        List<Integer> defines = section.getIntegerList("items."+name+".defines");        Character[] chars = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};     List<Character> charlist = Arrays.asList(chars);     for(int defined : defines)     {          Material md = Material.getMaterial(defined);                      recipe.setIngredient(charlist.get(0), md);     }      String[] rows = {r1, r2, r3};      recipe.shape(rows);        Bukkit.addRecipe(recipe);      }      catch(Exception error)     {                 }      }  }  public NewItem(List<String> lore, String name, int itemid, List<List<String>> lines, int damage, boolean hasRecipe, ConfigurationSection section)    {      this(lore, name, itemid, (byte)0, lines, damage, hasRecipe, section);  }  public Logger getLogger()  {      return Bukkit.getServer().getPluginManager().getPlugin("MakeYourOwnBlocks").getLogger(); }  public int getItem()   {      return id; }  public String getName()    {      return name;   }  public boolean onRightClick(Player p, boolean right, boolean block)    {      int isright;       int isblock;       if(right)      {          isright = 1;       }      else       {          isright = 0;       }      if(block)      {          isblock = 1;       }      else       {          isblock = 0;       }      try        {          List<String> cmds = commands.get(0);         for(String line : cmds)            {              line = line.replaceAll("\\<block\\>", String.valueOf(isblock));                line = line.replaceAll("\\<right\\>", String.valueOf(isright));                String[] segments = line.split(" ");             String cmd = segments[0];              List<String> rest = Arrays.asList(segments);             getLogger().info(String.valueOf(rest));                rest.remove(0);                StringBuilder sb = new StringBuilder();                for(String stst : rest)                {                  sb.append(stst);               }              String reststr = sb.toString();                if(cmd.equalsIgnoreCase("cmd"))              {                  Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), reststr);                }              if(cmd.equalsIgnoreCase("setblock"))             {                  Block b = p.getWorld().getBlockAt(Integer.parseInt(rest.get(0)), Integer.parseInt(rest.get(1)), Integer.parseInt(rest.get(2)));                    b.setTypeId(Integer.parseInt(rest.get(3)));                    if(rest.size() > 4)                 {                      b.setData((byte)Integer.parseInt(rest.get(5)));                    }              }              if(cmd.equalsIgnoreCase("say"))              {                  p.sendMessage(reststr);                }              if(cmd.equalsIgnoreCase("kill"))             {                  p.setHealth(0);                }              if(cmd.equalsIgnoreCase("hurt"))             {                  p.setHealth(p.getHealth()-Integer.parseInt(rest.get(0)));              }          }      }      catch(Exception error)     {          error.printStackTrace();           return false;      }      return true;   }  public boolean onLeftClick(Player p, boolean right, boolean block) {      int isright;       int isblock;       if(right)      {          isright = 1;       }      else       {          isright = 0;       }      if(block)      {          isblock = 1;       }      else       {          isblock = 0;       }      try        {          List<String> cmds = commands.get(1);         for(String line : cmds)            {              line.replaceAll("\\<block\\>", String.valueOf(isblock));               line.replaceAll("\\<right\\>", String.valueOf(isright));               String[] segments = line.split(" ");             String cmd = segments[0];              List<String> rest = Arrays.asList(segments);             rest.remove(rest.get(0));              StringBuilder sb = new StringBuilder();                for(String stst : rest)                {                  sb.append(stst);               }              String reststr = sb.toString();                if(cmd.equalsIgnoreCase("cmd"))              {                  Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), reststr);                }              if(cmd.equalsIgnoreCase("setblock"))             {                  Block b = p.getWorld().getBlockAt(Integer.parseInt(rest.get(0)), Integer.parseInt(rest.get(1)), Integer.parseInt(rest.get(2)));                    b.setTypeId(Integer.parseInt(rest.get(3)));                    if(rest.size() > 4)                 {                      b.setData((byte)Integer.parseInt(rest.get(5)));                    }              }              if(cmd.equalsIgnoreCase("say"))              {                  p.sendMessage(reststr);                }              if(cmd.equalsIgnoreCase("kill"))             {                  p.setHealth(0);                }              if(cmd.equalsIgnoreCase("hurt"))             {                  p.setHealth(p.getHealth()-Integer.parseInt(rest.get(0)));              }          }      }      catch(Exception error)     {          return false;      }      return true;   }}

A:

You need to implement java.io.Serializable:
public class NewItem implements Serializable

From JavaDoc:

Serializability of a class is enabled by the class implementing the
  java.io.Serializable interface. Classes that do not implement this
  interface will not have any of their state serialized or deserialized.
  All subtypes of a serializable class are themselves serializable. The
  serialization interface has no methods or fields and serves only to
  identify the semantics of being serializable.

A:

mark it as Serializable by implementing Serializable interface

