2014-03-12 163 views
0

我的代碼中有兩個數組列表。一個存儲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(); 
    } 

我讀了一些關於序列化,但它是我對它的第一次接觸。難道我做錯了什麼?

在此先感謝

+1

你什麼錯誤?複製並粘貼完整的堆棧跟蹤,而不是發送鏈接到屏幕截圖。 –

+0

'Location'不可序列化,這就是它所說的。使它可序列化,它從'Team'對象被引用 – hoaz

+0

我是個白癡,沒有注意到它......但是有一個問題,這個'Location'不是由我創建的,它屬於bukkit庫。有沒有辦法讓它可序列化? –

回答

0

你的問題是Location是不可序列:

java.io.NotSerializableException: org.bukkit.Location 

爲了使它序列化,首先將它轉換爲字符串:worldNamexyz,你可能使用這個:

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 
} 

然後,如果你想得到t他從字符串的位置,你可以這樣做:

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 
} 

通過這樣做,你現在已經轉換一個LocationString,使用locationToString(),你可以把一個數組,那麼,你可以採取String出來的陣列,並使用stringToLocation()將其轉換爲Location

+0

遠離計算機的幾個小時給了我一個類似於你的解決方案,它比turn位置可序列化更好。感謝您提供的所有幫助。 –

1

我剛纔

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; 
    } 
} 

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); 
    } 
}