Strange GWT compiler error when trying to serialize Java objects
Read the article on my new blog under http://blog.js-development.com/2008/08/strange-gwt-compiler-error-when-trying.html
I'm currently working on a project on my own where I'm developing a rich client by using the Google Web toolkit and a server using the Spring application framework. The client/server communication is implemented by using the GWT-RPC mechanism. Today however I experienced a strange compilation error when I started to do some experiments in serializing and transferring Java objects (POJOs) to the server. The GWT compiler issued a strange error saying
Type [yourpackagename.classname]The error is misleading and strange since my class actually implements the java.io.Serializable interface. By the way GWT doesn't really support the Serializable interface. It has just been introduced after some requests from developers to enable class reusing between applications. Prior to this change, classes had to implement the com.google.gwt.user.client.rpc.IsSerializable interface. I also tried that, with no success.was not serializable and has no concrete serializable subtypes.
import java.io.Serializable;
public class ItemLabel implements Serializable {
private String name;
public ItemLabel(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}Then I finally found the problem. It seems as if the serialization process needs an empty constructor. After changing my above code into the following, everything worked out fine.import java.io.Serializable;
public class ItemLabel implements Serializable {
private String name;
public ItemLabel(){}
public ItemLabel(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}


34 Comments:
I encountered a similar problem when using the XmlSerializer of c#. There you get an error at runtime which tells you that you must provice an empty constructor. For me it makes also sense, that the serializers want an empty constructor; because I think first they create an object of the needed type (here is the empty constructor needed because there are no parameters available) and then they fill in the properties with reflection.
I know, I also encountered the same problem when serializing objects to XML in C#. That was actually also the reason why I tried at some point to add an empty constructor to my class.
The error message produced in C# is however much more concrete and one knows immediately what has to be done. The problem here with the GWT was the strange message ("Type [yourpackagename.classname] was not serializable and has no concrete serializable subtypes") which was completely nonsense in my case since I implemented the IsSerializable interface. Later I've also found a bug-report on the GWT toolkit that someone requested to throw a more meaningful message but it seems as if they have not implemented it yet.
> It seems as if the serialization process needs an empty constructor.
Uumm, yes, of course. That's a requirement put forth by the specification of java.io.Serializable: http://java.sun.com/j2se/1.4.2/docs/api/java/io/Serializable.html. If you think about it it makes a lot of sense. Otherwise, serializable class cannot be instantiated via reflection automatically - only if you know which constructor argument is which.
Thanks !
Encountered this problem today. Error is pretty missleading but offcourse it is normal. They should change the error text for this one in a future release.
hii, i like the article you wrote. i also wrote an article on Serialization here : http://kaniks.blogspot.com
feel free to post your comments
thanks
cheers
Thx buddy!,
U saved me.
good one! thanks!
Thanks this really helped.
Thanks !!! Saved me hours of frastration
Really helpful tip. Thx!!!
I'm adding to the thanks train. Saved me a lot of trouble.
thanks buddy
If you change your debuglevel to SPAM when running your app in hosted mode, the compiler will give you better feedback. for example, it will tell you if you are missing a default constructor, or if you forgot to implement IsSerializeable
@mrgoldenbrown:
Thanks for your suggestion. Didn't know that.
That was really helpful! Thanks!
Thanks a lot! Exactly my problem.
Thanks a lot. Constructor was an issue
Dude, for making the mistake first and posting the answer you are awesome! Thanks :)
:) I was young and unexperienced :) The reason of the error is obvious if you think a moment. But still it seems quite a lot of people are having this issue ;)
Implementing a better description of the compiler error would help a lot, but I guess their not doing it any more..I mean, all those people are landing here anyway, so :)
Note that complier will also print this error if your class has any custom type fields that are not properly prepared for serialization.
Thanks. This was just answer I was looking for.
Thanks very much. Had exactly the same problem.
Thanks a lot.
This error was driving me mad. Thanks for posting the solution.
Excellent, Thank you!
you are my hero!
Great!
thanks a lot!
Thanks, you save me some time. That deserves a better error message I think. empty constructor on a Serializable Class is a classic mistake.
@Francois
Yes definitely. I wonder why they didn't yet include it.
Thanks a lot!!
Thanks a lot! You helped to fix my issue
thanks a lot. saved my day.
BTW, it is a default constructor (no input parms) needed.
i wasted my time with the same issue.
great input here!! thanks a lot,
Thank you very much, You keep me to not waste my time.
Post a Comment