GSON complains of lack of no-args constructor despite it existing

1,470 views
Skip to first unread message

Paul Kilgo

unread,
Jun 28, 2010, 2:52:57 PM6/28/10
to google-gson
Hello

I have a pretty simple container class called Game with a contained
class Player which looks something like this:

public class Game {
[...]
public class Player {

public Player() { }
public String id;
public String score;

}
}

I get the error when trying to cast to Game:
No-args constructor for class com.reddit.worddit.api.response.Game
$Player does not exist. Register an InstanceCreator with Gson for this
type to fix this problem.


The source JSON looks something like:
game[]
[...]
players[]:
id: string
score: string

I understand the error (I think) but it does have a no-args
constructor. I could make an instance creator but I would think it
wouldn't be necessary in this case. Any thoughts on this?

inde...@gmail.com

unread,
Jun 28, 2010, 4:01:45 PM6/28/10
to google-gson
Player() looks like a no-args constructor but it isn't. The reason is
because Player is an inner class (not a static inner class) which
needs a Game instance before you can instantiate a Player instance.
You can address this by either declaring Player to be a static inner
class, or by providing an instance creator.

HTH
Inder

Paul Kilgo

unread,
Jun 28, 2010, 4:14:19 PM6/28/10
to google-gson
You were absolutely correct. Thank you!
Reply all
Reply to author
Forward
0 new messages