我正在尝试从文件中反序列化一个对象,但是当我这样做时,我得到以下堆栈跟踪:
21.04 10:33:18 [Server] WARN at java.lang.Thread.run(Unknown Source)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696)
21.04 10:33:18 [Server] WARN at java.util.concurrent.FutureTask.run(Unknown Source)
21.04 10:33:18 [Server] WARN at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950)
21.04 10:33:18 [Server] WARN at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115)
21.04 10:33:18 [Server] WARN at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646)
21.04 10:33:18 [Server] WARN at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
21.04 10:33:18 [Server] WARN at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
21.04 10:33:18 [Server] WARN at me.trevor1134.squads.SquadExecutor.onCommand(SquadExecutor.java:47)
21.04 10:33:18 [Server] WARN at me.trevor1134.squads.command.SquadCommand.onCommand(SquadCommand.java:32)
21.04 10:33:18 [Server] WARN at me.trevor1134.squads.command.CreateCommand.run(CreateCommand.java:24)
21.04 10:33:18 [Server] WARN at me.trevor1134.squads.Squadron.isSquad(Squadron.java:45)
21.04 10:33:18 [Server] WARN at me.trevor1134.squads.SquadWriter.isSquad(SquadWriter.java:43)
21.04 10:33:18 [Server] WARN at me.trevor1134.squads.SquadWriter.getSquad(SquadWriter.java:33)
21.04 10:33:18 [Server] WARN at java.io.ObjectInputStream.readObject(Unknown Source)
21.04 10:33:18 [Server] WARN at java.io.ObjectInputStream.readObject0(Unknown Source)
21.04 10:33:18 [Server] WARN at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
21.04 10:33:18 [Server] WARN java.io.EOFException
我查看了导致错误的代码,发现了这个:
(isSquad):
public static boolean isSquad(String squad) {
try {
return getSquad(squad) != null; // Line 43
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
} }
(getSquad):
public static Squad getSquad(String squad) throws ClassNotFoundException, FileNotFoundException, IOException {
File f = new File("plugins/Squadron/Squads/" + squad + ".dat");
if (!f.exists()) {
return null;
}
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
if (in.readObject() instanceof Squad) {
Squad s = (Squad) in.readObject(); //Line 33
in.close();
return s;
}
in.close();
return null;
}
我不明白这个问题,因为文件已创建并立即使用小队对象序列化。
答案 0 :(得分:1)
您的问题是,当您只有一个要阅读的对象时,您正在调用readObject
两次。
您需要更改此if-block
:
if (in.readObject() instanceof Squad) {
Squad s = (Squad) in.readObject(); //Line 33
in.close();
return s;
}
这样的事情:
Object readObject = in.readObject();
if (readObject instanceof Squad) {
Squad s = (Squad) readObject; //Line 33
in.close();
return s;
}