我的代碼中有兩個數組列表。一個存儲User對象,另一個存儲Team對象。我試圖做的是什麼時候關閉指令給兩個數組列表保存到文件,然後關閉程序。自定義對象序列化的Arraylist
我在保存兩個AL的過程中遇到了問題,但後來我定義了一個serialVersionUID,只有User AL us正常工作。我仍然從AL隊得到一個錯誤。
保存時出錯:
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
加載錯誤:
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)
這是我的世界插件,我目前正在開發的一部分。 一些代碼:
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();
}
我讀了一些關於序列化,但它是我對它的第一次接觸。難道我做錯了什麼?
在此先感謝
你什麼錯誤?複製並粘貼完整的堆棧跟蹤,而不是發送鏈接到屏幕截圖。 –
'Location'不可序列化,這就是它所說的。使它可序列化,它從'Team'對象被引用 – hoaz
我是個白癡,沒有注意到它......但是有一個問題,這個'Location'不是由我創建的,它屬於bukkit庫。有沒有辦法讓它可序列化? –