-4

I've got a NotSerializableException, and the jvm is telling me it is coming from a class that has absolutely nothing to do with what is getting serialized to the ObjectOutputStream!

What on earth could be going on? The class it mentions as the problem is NOT serializable, and manipulates some data from OTHER classes which most definitely ARE serializable, why on earth would this result in a NotSerializableException?

EDIT: Please dont vote down this is a serious question! Ok, so i'm just wondering whether the following could be a problem: If i had an anonymous arraylist, being inserted into a map, (which most definitely IS serializable), in a class which is NOT serializable, could this be the problem????

like so: map.put(new ArrayList(){{add(ect....}});

Sam Adamsh
  • 3,331
  • 8
  • 32
  • 53
  • 3
    What's the full stack trace? What object are you trying to serialize (post the code)? – Jeffrey Apr 29 '12 at 16:38
  • 1
    Fields in the class you want to serialize must be serializable too. – Willem Van Onsem Apr 29 '12 at 16:41
  • that is NOT THE PROBLEM!!! i have had absolutely NO ISSUE serializing the class! i just started manipulating it differently inside a different file, and all of a sudden im getting this exception – Sam Adamsh Apr 29 '12 at 16:42
  • how do i post a screenshot, the stack trace is on the command line and i cant copy/paste – Sam Adamsh Apr 29 '12 at 16:43
  • 1
    @Sam: without more details on your code, no one can say anything except that you have a bug. – Mat Apr 29 '12 at 16:43
  • 3
    You can most definitely copy / paste from a command line. Please don't post a screenshot, god kills a kitten every time someone does that. – Chris Eberle Apr 29 '12 at 16:45
  • @Sam: you can copy from a cmd.exe window (assuming that's what you're talking about). Look in the window menu, there's a select/copy thing somewhere in there – Mat Apr 29 '12 at 16:45
  • You can copy past stack traces from a console window or from the IDE window. In Linux it's Ctrl+Shift+C and I think Windows enables it in the context menu of the Window. Secondly perhaps the program never needed to serialize before. Checking if serialization is possible can only happens at runtime. Therefore you should follow the tree of fields (fields of the classed that are fields of your class) to make sure that all the data is serializable. – Willem Van Onsem Apr 29 '12 at 16:45
  • its in taskeng.exe and i dont see a menu, sorry not cmd. i just tried ctr shift c and it just terminated batch job, and now its gone. so that obviously doesnt work, my bad. – Sam Adamsh Apr 29 '12 at 16:47
  • 1
    You can run the program in a proper IDE? NetBeans runs the program in a textbox where you can copy/paste. – Willem Van Onsem Apr 29 '12 at 16:50
  • i think i figured it out, view edited question – Sam Adamsh Apr 29 '12 at 16:58

1 Answers1

4

Fields also have to be Serializable. A gotcha is that nested classes have a reference to their outer class.

 class NotSerializable {
     class Inner implements Serializable {
         // contains a reference to this$0, the outer instance
     }

 }

i.e. you have a field you might not be aware of.

The simple solution is to make the inner class static

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I think collections are pitfalls too (since the collection itself is only serializable when the type it stores is serializable). Strange there isn't any IDE that checks automatically if the type is serializable. – Willem Van Onsem Apr 29 '12 at 16:55
  • i think this was the issue, i had an anonymous arraylist, which is a type of inner class, sitting in a class which was not serializable, just didnt realize it – Sam Adamsh Apr 29 '12 at 17:01
  • 1
    If you create the anonymous class in a `static` method (or move it to a static method) it won't have the implied reference. – Peter Lawrey Apr 29 '12 at 17:15