-1

似乎我无法序列化我的名为“Ban”的课程:

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

5 回答 5

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 时,您也在序列化这个不可序列化的隐式实例,因此出现异常。

使 Ban 成为顶级类或静态内部类。

于 2013-04-03T17:44:25.357 回答
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}

请注意,即使我给了它们值,输出的两个日期都为空。瞬态值未序列化

于 2013-04-03T17:28:12.717 回答
0

Java中的transient关键字用于指示不应序列化字段。有关更多详细信息,请参阅这篇文章为什么 Java 有瞬态字段?

没有您的错误 cpde,我们无法为您提供帮助

于 2013-04-03T17:25:29.670 回答
0

瞬态变量不能被序列化。

于 2013-04-03T17:28:14.480 回答
0

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

于 2013-04-03T17:45:02.857 回答