0

I would like to change my qestion to the following:

I have one HashMap object, and one int. I want to serialize the into the same file and get back. I know that I should do deserialization in the same order as I have done serialization.

Please provide me with a code which performes that action.

7
  • He's saying, don't you think you should say what the exception is?? Commented Sep 17, 2009 at 13:06
  • I said already that it is written "Error: java.util.HashMap". Commented Sep 17, 2009 at 13:08
  • 4
    @Narek, Can you give the full stack trace of the exception. There should be more than just that line. Commented Sep 17, 2009 at 13:11
  • printStackTrace() says that when I an calling readObject(), it is failing to read back the HashMap. Commented Sep 17, 2009 at 13:25
  • 1
    @Narek, can you provide the full stack trace as an edit to your question? That will help us with your problem. The code as you presented it in your question will serialize and deserialize fine, so there is something else going on (either there is more to your code that we are not seeing, or the whole stack trace will give us better insight). Commented Sep 17, 2009 at 18:21

2 Answers 2

6

I am making an assumption based on the information you have provided that you are having serialization issues based on incompatibilities between the originally serialized class and an updated class with a new field.

If you have previously serialized a Class without specifying a private static long serialVersionUID, adding a new field will result in an InvalidClassException when trying to read the file back.

Note that simply adding the serialVersionUID after you have already serialized the previous version will not result in a compatible class. What you need to do is regenerate the original serialVersionUID, grab that number and apply it to your class.

The Steps you must take:

  1. remove the new int (for now) and compile
  2. generate the original serialVersionUID by running serialVer against the compiled class
  3. take the resulting number and assign it to the private static long serialVersionUID that you are applying to your updated class
  4. re-add the new int.

After these steps, you should be ok to deserialize the original.

recommended reading: why should I bother about serialVersionUID (sic)

Sign up to request clarification or add additional context in comments.

2 Comments

If you have existing data, you need to set the serialVersionUID to that of the version of the class that created the data (use the serialver tool).
(If you've lost the original class files, you should be able to find another question on stackoverflow that explains how you can go about recovering them.)
1

Without a stack trace, it is hard to guess the exact problem, but I'm going to go out on a limb and suggest two possibilities:

The hashmap at runtime doesn't contain serializable objects. Given the generic type that you present, this would only be possible if a raw type was used. Such as:

public void setMap(HashMap map) { this.map = map; }

That would give you a compiler warning, but it would compile so something like that may be going on. The same thing would happen if you had a getter on the map, and something referenced the map using its raw type. Say:

public Map<String, String> getMap() { return map; }

Map m = getMap();
m.put(new Object(), new Object());

The other possibility I can think of is that you are corrupting the file in some way when you write or read it (say doing something with file encoding or not closing the stream when you write it).

Without more information (the complete stack trace - cut out the lines referencing your own project name if you need, but no others) and the code which does the serialization and deserialization, it is impossible to get further insight into the problem.

By the way, no comment or answer thinks that you serialized a HashMap and want to deserialize MyClass (it isn't really possible - you can't decide what class you get when you deserialize). I think you are misunderstanding the comments. What they are talking about is different versions of MyClass as they have existed over time. I'm guessing in my answer that this is not the case (you are in fact serializing MyClass fresh during the running of the code and deserializing without any recompilation in-between. If not (if in fact you serialized the class, changed it, and recompiled and are trying to re-read an old file), then akf's answer with Tom's comments are on the right track.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.