Hussein Yassine Hussein Yassine - 7 months ago 24
Android Question

Gson crashes when writing object

I've used Gson many times and this is the first time I face such a problem, once I want to write the object to the shared preferences, the app crashes without any exception in the logcat can you please help me

I'm saving the object like this (like I always do):

mShared = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
edit = mShared.edit();
Gson gson = new Gson();
String json = gson.toJson(currentTeam);
edit.putString(getResources().getString(R.string.current_team), json).apply();


here are the classes:

GameType.java

public class GameType implements Parcelable{

private int id;
private String lookupCode, name, namePS, icon;
private List<Team> teams = new ArrayList<Team>();

public GameType(){

}

public List<Team> getTeams() {
return teams;
}

public void setTeams(List<Team> teams) {
this.teams = teams;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getLookupCode() {
return lookupCode;
}

public void setLookupCode(String lookupCode) {
this.lookupCode = lookupCode;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getNamePS() {
return namePS;
}

public void setNamePS(String namePS) {
this.namePS = namePS;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

dest.writeInt(id);

dest.writeString(lookupCode);
dest.writeString(name);
dest.writeString(namePS);
dest.writeString(icon);

dest.writeList(teams);
}

// "De-parcel object
private GameType(Parcel in) {

id = in.readInt();

lookupCode=in.readString();
name=in.readString();
namePS = in.readString();
icon = in.readString();

in.readList(teams,getClass().getClassLoader());
}

public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public GameType createFromParcel(Parcel in) {
return new GameType(in);
}

public GameType[] newArray(int size) {
return new GameType[size];
}
};

}


Team.java

public class Team implements Parcelable{

private int id;
private String namePs, logo , nameEn, textColor, firstColor, secondColor, thirdColor;
private GameType game;

public Team(){

id = -1;
namePs = "";
logo = "";
nameEn = "";
textColor = "";
firstColor = "";
secondColor = "";
thirdColor = "";
game = new GameType();
}

public Team(int id, String logo, String namePs, String nameEn, String textColor, String firstColor, String secondColor, String thirdColor) {
this.id = id;
this.namePs = namePs;
this.nameEn = nameEn;
this.logo = logo;

this.textColor = textColor;
this.firstColor = firstColor;
this.secondColor = secondColor;
this.thirdColor = thirdColor;
}

public void setGame(GameType game) {
this.game = game;
}

public GameType getGame() {
return game;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getNamePs() {
return namePs;
}

public void setNamePs(String nameFa) {
this.namePs = nameFa;
}

public String getNameEn() {
return nameEn;
}

public void setNameEn(String nameEn) {
this.nameEn = nameEn;
}

public String getLogo() {
return logo;
}

public void setLogo(String logo) {
this.logo = logo;
}

public String getTextColor() {
return textColor;
}

public void setTextColor(String textColor) {
this.textColor = textColor;
}

public String getFirstColor() {
return firstColor;
}

public void setFirstColor(String firstColor) {
this.firstColor = firstColor;
}

public String getSecondColor() {
return secondColor;
}

public void setSecondColor(String secondColor) {
this.secondColor = secondColor;
}

public String getThirdColor() {
return thirdColor;
}

public void setThirdColor(String thirdColor) {
this.thirdColor = thirdColor;
}


@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

dest.writeInt(id);

dest.writeString(namePs);
dest.writeString(logo);
dest.writeString(nameEn);
dest.writeString(textColor);
dest.writeString(firstColor);
dest.writeString(secondColor);
dest.writeString(thirdColor);
dest.writeParcelable(game, flags);
}

// "De-parcel object
private Team(Parcel in) {

id = in.readInt();

namePs = in.readString();
logo = in.readString();
nameEn = in.readString();
textColor = in.readString();
firstColor = in.readString();
secondColor = in.readString();
thirdColor = in.readString();

game = in.readParcelable(getClass().getClassLoader());
}

public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public Team createFromParcel(Parcel in) {
return new Team(in);
}

public Team[] newArray(int size) {
return new Team[size];
}
};
}


Here's the Log cat:

FATAL EXCEPTION: main
Process: com.ids.a3090, PID: 20391
java.lang.StackOverflowError: stack size 8MB
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:148)
at java.lang.StringBuffer.append(StringBuffer.java:219)
at java.io.StringWriter.write(StringWriter.java:147)
at com.google.gson.stream.JsonWriter.string(JsonWriter.java:566)
at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:417)
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:422)
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:406)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(Refle

Answer Source

You have a circular reference. Team references on GameType which has this Team.