Forums Register Login

what are the reasons for: invalidclassexception?

+Pie Number of slices to send: Send
hey,
i have a sequential access file with objects given to me by my instructor. he has asked that we "duplicate" the class he created. the only fields in the class are a couple of String(s),an int, and 3 accessor methods. when i try to read the from the stream i get the exception above. now he insists that i don't have to set the serialVersionUID in my version of his class, which i have found will work, but that i spend time trying to mimic the constructor and method signatures he used originally and that this will 'compute' to the same serialVersionUID. my question(s): is he correct? and what are the possible causes/reasons for an InvalidClassException? any salient help will... thanks

nikita
+Pie Number of slices to send: Send
The API documentation for InvalidClassException says:


Thrown when the Serialization runtime detects one of the following problems with a Class.

  • The serial version of the class does not match that of the class descriptor read from the stream
  • The class contains unknown datatypes
  • The class does not have an accessible no-arg constructor
  •  
    +Pie Number of slices to send: Send
    In general, the serialization mechanizm is enough robust, and will successfully load your class even if there are some changes in the code and/or fields, but the implementation protects you, providing some hash value that catches the changes done in the code of your class. The serialVersionUID is the way to declare the value by hand, if you can guarantee that you version of the class is enough compatible with previous versions.
    So I guess your instructor is correct if your changes to the class do not result in an inability to de-serialize.
    [ September 13, 2008: Message edited by: Satya Maheshwari ]
    +Pie Number of slices to send: Send
    okay.....
    but is it true that if i duplicate the class as he originally defined it, it will hash out to the same serialVersionUID as the class objects in the file. here's what i have so far:



    and a test run:



    i didn't use a try/catch block in order to show the exception. if anyone wants "yankees.yanks", pm me and i'll gladly give it to ya. if i take out the constant, it hashes to a different value. this should have been a excercise in rote, but like most things i have learned in java
    there is always something more... so if anyone can explain the mechanism behind this, (like how java creates the serial version), i'd really appreciate it. take care.
    [ September 13, 2008: Message edited by: f. nikita thomas ]
    +Pie Number of slices to send: Send
    found the following here:
    http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html#4100



    so...





    i deleted the .class file and recompiled:



    so my instructor was correct. then i decided to change the constructor signature:



    and then:



    i changed the identifiers as well which had no effect. i changed the access modifiers and:





    this is the same id that i found during reading the stream and receiving an exception. so i learned something.

    btw, i didn't see the need to move this post to the intermediate section. i'm an undergraduate at a community college. i feel that the subject material would have been better left there. take care.
    [ September 13, 2008: Message edited by: f. nikita thomas ]
    +Pie Number of slices to send: Send
     

    so i learned something



    Great. Same here.
    Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 905 times.
    Similar Threads
    ArrayList problem ...
    getting around an invalidClassException
    Why does serialization require a serial version UID in the subclasses of the implementing class.
    Deserialization problem - InvalidClass w/ negative serialVersionUid for local class
    InvalidClassException
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 03:43:09.