-1

It seems like I can't serialize my class called "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;
        }
    }

When I try to print this with a objectoutputstream it gives me an ERROR... Could you please help me by serializing this class? BTW: What does transient mean?

well it is bukkit but this should do it:

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

My saving code looks like this:

The variable:

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

The saving part:

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(banned);
oos.flush();
oos.close();
Henry Mü
  • 91
  • 6
  • 1
    What error does it give you? `transient` means that the property should not be serialized. – Vivin Paliath Apr 03 '13 at 17:22
  • What is the error message? Copy and paste it into your question. You probably need to add a public default constructor. – Thorn Apr 03 '13 at 17:22
  • 1
    for transient variable see this:- http://stackoverflow.com/questions/5245600/what-does-the-keyword-transient-means-in-java – Avinesh Apr 03 '13 at 17:25
  • 1
    `transient` definition can be found here [link](http://stackoverflow.com/questions/5245600/what-does-the-keyword-transient-means-in-java) – Khanh Tran Apr 03 '13 at 17:26

5 Answers5

2

It seems you have defined Ban as a non-static inner class of another class called Main:

public class Main {

    class Ban {
        ...
    }
}

This means that every Ban instance has an implicit reference to its outer Main instance. The code above is similar to the following one:

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

or public class Main { }

class Ban {
    private Main main;
}

So when you serialize a Ban, you're also serializing this implicit instance, which is not serializable, hence the exception.

Make Ban a top-level class, or a static inner class.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

There is nothing wrong with the Ban class as posted. Maybe the error was produced by your class including a Main field variable that is not serializable. But the code posted in the question can be serialized; We can run this:

public 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;
  }

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

}

The output produced is:

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

Note that the output has both dates as null even though I gave them values. A transient value is not serialized.

Thorn
  • 4,015
  • 4
  • 23
  • 42
  • I ran the code without an error. If I got the same exception you did then the output pasted into my answer would not have been produced. Try copying and pasting my code and running it. – Thorn Apr 03 '13 at 17:41
0

The transient keyword in Java is used to indicate that a field should not be serialized. See this post for more details Why does Java have transient fields?

Without your error cpde we cannot assist you

Community
  • 1
  • 1
evgenyl
  • 7,837
  • 2
  • 27
  • 32
0

transient variables cannot be serialized.

PranitG
  • 121
  • 8
  • Can you clarify how/why this is? – Stuart M Apr 03 '13 at 17:46
  • transient is a Java keyword specifically created to exclude variables from serialization. Remove it and you can potentially serialize a variable. There are many beginner introductions to the topic try the SCJP book by Bates and Sierra – Crowie Sep 03 '13 at 08:50
0

The transient fields are not serialized, they will be null after reading from a stream. The error seems to come from an inner class named Main which is missing in your code example and not implement Serializable!

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94