2016-03-01 41 views
-1

因此,我正在爲Minecraft Spigot編寫一個插件,但我得到一個Java錯誤,所以這就是爲什麼我沒有發佈在Minecraft插件論壇上,但無論如何我試圖保存一個名爲GroupManager類成文件(被稱爲目標數據類) 之後,我想讀一遍,這是我的錯誤信息:寫入文件時出現ObjectInput和OutputStream錯誤

[18:43:38 WARN]:  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) 
[18:43:38 WARN]:  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) 
[18:43:38 WARN]:  at playerapi.cubenex.ObjectData.write(ObjectData.java:27) 
[18:43:38 WARN]:  at playerapi.cubenex.GroupManager.save(GroupManager.java:70) 
[18:43:38 WARN]:  at playerapi.cubenex.ThePlugin.onDisable(ThePlugin.java:70) 
[18:43:38 WARN]:  at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:323) 
[18:43:38 WARN]:  at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:364) 
[18:43:38 WARN]:  at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:424) 
[18:43:38 WARN]:  at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:417) 
[18:43:38 WARN]:  at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:458) 
[18:43:38 WARN]:  at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:708) 
[18:43:38 WARN]:  at org.bukkit.Bukkit.reload(Bukkit.java:535) 
[18:43:38 WARN]:  at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) 
[18:43:38 WARN]:  at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) 
[18:43:38 WARN]:  at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) 
[18:43:38 WARN]:  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
[18:43:38 WARN]:  at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) 
[18:43:38 WARN]:  at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) 

這裏的方法,當我保存

public void save(){ 
    data.write(this); 
} 

當我讀

public static GroupManager getGroupManager(Plugin plugin){ 
    DataFile file = new DataFile("groups.dot", plugin); 
    if (!file.exists()) return new GroupManager(plugin); 
    ObjectData data = new ObjectData(file); 
    return (GroupManager) data.read(); 
} 

GroupManager類。 而這裏的保存(/寫)

public void write(Object obj){ 
    file.delete(); 
    try { 
     file.create(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     FileOutputStream fos = new FileOutputStream(file); 
     ObjectOutputStream oos = new ObjectOutputStream(fos); 
     oos.writeObject(obj); 
     oos.flush(); 
     fos.close(); 
     oos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

的方法,當我讀

public Object read(){ 
    try { 
     FileInputStream fis = new FileInputStream(file); 
     ObjectInputStream ois = new ObjectInputStream(fis); 
     Object obj = ois.readObject(); 
     fis.close(); 
     ois.close(); 
     return obj; 
    } catch (IOException | ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 

(這些都是從目標數據類的方法) 如果你需要在這裏的GroupManager類是:

public class GroupManager { 

ObjectData data; 

private ArrayList<Group> groups; 
private HashMap<Player, Group> players; 

public GroupManager(Plugin plugin){ 
    data = new ObjectData(new DataFile("groups.dot", plugin)); 
    groups = new ArrayList<Group>(); 
} 

public void add(Player p, Group g){ 
    players.put(p, g); 
    if (p.isOnline()) g.addPlayer(p); 
} 

public void Joined(Player p){ 
    players.get(p).addBackPlayer(p); 
} 

public Group getGroup(Player p){ 
    return players.get(p); 
} 

public void registerGroup(Group group){ 
    groups.add(group); 
} 

public void deleteGroup(Group group){ 
    groups.remove(group); 
} 

public boolean containsGroup(Group group){ 
    return groups.contains(group); 
} 

public boolean containsGroup(String name){ 
    boolean r = false; 
    for (int i = 0; i < groups.size(); i++){ 
     if (groups.get(i).getGroupName().equalsIgnoreCase(name)) r = true; 
    } 
    return r; 
} 

public Group getGroup(String name){ 
    Group r = null; 
    for (int i = 0; i < groups.size(); i++){ 
     if (groups.get(i).getGroupName().equalsIgnoreCase(name)) r = groups.get(i); 
    } 
    return r; 
} 

public static GroupManager getGroupManager(Plugin plugin){ 
    DataFile file = new DataFile("groups.dot", plugin); 
    if (!file.exists()) return new GroupManager(plugin); 
    ObjectData data = new ObjectData(file); 
    return (GroupManager) data.read(); 
} 

public void save(){ 
    data.write(this); 
} 
+3

您的堆棧跟蹤缺少第一行,這非常重要... –

+1

您發佈了除最重要部分之外的所有內容:堆棧跟蹤的第一行,告訴異常是什麼。在這種情況下很容易猜到(NotSerializableException),但下次請發佈該信息。並閱讀它。如果你已經閱讀過它,然後閱讀NotSerializableException的javadoc,那麼你會得到你的答案。 –

回答

2

落實Serializable接口

public class GroupManager implements Serializable { 
+0

並確保Group,Player,ObjectData及其所有字段以遞歸方式實現它。 –