不能序列化我的Java类

时间:2013-04-03 17:21:18

标签: java class serialization

好像我无法将我的班级序列化为“禁令”:

class Ban implements Serializable{
        /**
         * 
         */
        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;
        }
    }

当我尝试使用objectoutputstream打印时,它会给我一个错误... 你可以通过序列化这门课来帮助我吗?顺便说一句:瞬态意味着什么?

这是bukkit但是应该这样做:

[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)

我的保存代码如下所示:

变量:

public static HashMap<String, Ban> banned = new HashMap<String, Ban>();

保存部分:

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(banned);
oos.flush();
oos.close();

5 个答案:

答案 0 :(得分:2)

似乎你已经将Ban定义为另一个名为Main的类的非静态内部类:

public class Main {

    class Ban {
        ...
    }
}

这意味着每个Ban实例都有一个对其外部Main实例的隐式引用。上面的代码类似于以下代码:

public class Main {
    static class Ban {
        private Main main;
    }
}

或     公共类Main {     }

class Ban {
    private Main main;
}

因此,在序列化Ban时,您还要序列化此隐式实例,该实例不可序列化,因此是例外。

禁止使用顶级课程或静态内部课程。

答案 1 :(得分:1)

发布的Ban类没有任何问题。也许错误是由您的类产生的,包括一个不可序列化的Main字段变量。但是问题中发布的代码可以序列化;我们可以运行:

公共类Ban实现Serializable {

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

}

产生的输出是:

 Ban{start=null, end=null, explination=Transient means do not serialize., from=Thorn}

请注意,即使我给它们赋值,输出的日期也都为null。 瞬态值未序列化。

答案 2 :(得分:0)

Java中的transient关键字用于指示不应序列化字段。 有关详细信息,请参阅此帖子Why does Java have transient fields?

没有您的错误cpde我们无法帮助您

答案 3 :(得分:0)

瞬态变量无法序列化。

答案 4 :(得分:0)

transient字段未序列化,从流中读取后将为null。该错误似乎来自一个名为Main的内部类,它在您的代码示例中缺失,而不是实现Serializable