cant serialize my class Java
似乎我无法序列化名为"ban"的类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
当我尝试用ObjectOutputStream打印这个时,它会给我一个错误…你能帮我把这门课连载一下吗?顺便问一下:瞬变是什么意思?
好吧,这是布基特,但这应该做到:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [SEVERE] java.io.NotSerializableException: me.bubblegumsoldier.Ban.Main 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.util.HashMap.writeObject(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 2013-04-03 19:30:30 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.lang.reflect.Method.invoke(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeObject0(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at java.io.ObjectOutputStream.writeObject(Unknown Source) 2013-04-03 19:30:30 [SEVERE] at me.bubblegumsoldier.Ban.Main.save(Main.java:113) 2013-04-03 19:30:30 [SEVERE] at me.bubblegumsoldier.Ban.Main.onDisable(Main.java:247) 2013-04-03 19:30:30 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219) 2013-04-03 19:30:30 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481) 2013-04-03 19:30:30 [SEVERE] at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:400) 2013-04-03 19:30:30 [SEVERE] at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:393) 2013-04-03 19:30:30 [SEVERE] at org.bukkit.craftbukkit.v1_5_R2.CraftServer.disablePlugins(CraftServer.java:277) 2013-04-03 19:30:30 [SEVERE] at net.minecraft.server.v1_5_R2.MinecraftServer.stop(MinecraftServer.java:331) 2013-04-03 19:30:30 [SEVERE] at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:438) 2013-04-03 19:30:30 [SEVERE] at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573) |
号
我的保存代码如下:
变量:
保存部分:
1 2 3 4 | ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(banned); oos.flush(); oos.close(); |
。
似乎您已经将ban定义为另一个名为main的类的非静态内部类:
1 2 3 4 5 6 | public class Main { class Ban { ... } } |
这意味着每个BAN实例都有一个对其外部主实例的隐式引用。以上代码与以下代码类似:
1 2 3 4 5 | public class Main { static class Ban { private Main main; } } |
。
或公共类主{}
1 2 3 | class Ban { private Main main; } |
所以当您序列化一个BAN时,您也在序列化这个隐式实例,它是不可序列化的,因此是异常的。
使ban成为顶级类或静态内部类。
发布的禁令类没有任何问题。可能错误是由类产生的,其中包含一个不可序列化的主字段变量。但问题中发布的代码可以序列化;我们可以运行以下代码:
公共类禁止实现可序列化{
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | transient Date start; transient Date end; String explination; String from; public Ban(Date s, Date e, String ex, String f) { start = s; end = e; explination = ex; from = f; } public String toString() { return"Ban{" +"start=" + start +", end=" + end +", explination=" + explination +", from=" + from + '}'; } public static void main(String[] args) throws IOException, ClassNotFoundException { Ban x = new Ban(new Date(), new Date(),"Transient means do not serialize.","Thorn"); File f = new File("test.out"); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f)); out.writeObject(x); out.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream(f)); Ban fromFile = (Ban) in.readObject(); System.out.println(fromFile); in.close(); } |
}
产生的输出为:
1 | Ban{start=null, end=null, explination=Transient means do not serialize., from=Thorn} |
号
注意,输出的两个日期都为空,即使我给了它们值。未序列化临时值。
无法序列化临时变量。
Java中的n& nbsp关键字用于指示字段不应该序列化。请参阅这篇文章,了解Java为什么有瞬态字段?
如果没有你的错误,我们无法帮助你。