Dear reader of Juri's TechBlog,
I moved my blog to a new domain and a new hosting solution as well. I'm now blogging on juristr.com.

Strange GWT compiler error when trying to serialize Java objects

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] was not serializable and has no concrete serializable subtypes.
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.
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;
 }
 
}

Posts you might also be interested in..

Credits: Hoctro | Jack Book

34 Comments:

Manfred said...

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.

Juri Strumpflohner said...

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.

Marcel said...

> 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.

Frank said...

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.

NIks said...

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

Anonymous said...

Thx buddy!,
U saved me.

Johannes Schaback said...

good one! thanks!

Mukesh said...

Thanks this really helped.

Vova said...

Thanks !!! Saved me hours of frastration

Anonymous said...

Really helpful tip. Thx!!!

Anonymous said...

I'm adding to the thanks train. Saved me a lot of trouble.

vijay said...

thanks buddy

mrgoldenbrown said...

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

Juri Strumpflohner said...

@mrgoldenbrown:
Thanks for your suggestion. Didn't know that.

NeMeSiS said...

That was really helpful! Thanks!

Ben Northrop said...

Thanks a lot! Exactly my problem.

Anonymous said...

Thanks a lot. Constructor was an issue

Ben Daniel said...

Dude, for making the mistake first and posting the answer you are awesome! Thanks :)

Juri Strumpflohner said...

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

smok said...

Note that complier will also print this error if your class has any custom type fields that are not properly prepared for serialization.

Lee said...

Thanks. This was just answer I was looking for.

Simon said...

Thanks very much. Had exactly the same problem.

Anonymous said...

Thanks a lot.

www.netthreads.co.uk said...

This error was driving me mad. Thanks for posting the solution.

Sam Brodkin said...

Excellent, Thank you!

Anonymous said...

you are my hero!

yasu said...

Great!
thanks a lot!

François said...

Thanks, you save me some time. That deserves a better error message I think. empty constructor on a Serializable Class is a classic mistake.

Juri Strumpflohner said...

@Francois
Yes definitely. I wonder why they didn't yet include it.

Anonymous said...

Thanks a lot!!

butcherillo said...

Thanks a lot! You helped to fix my issue

Anonymous said...

thanks a lot. saved my day.
BTW, it is a default constructor (no input parms) needed.

Anonymous said...

i wasted my time with the same issue.
great input here!! thanks a lot,

Anonymous said...

Thank you very much, You keep me to not waste my time.

Post a Comment