java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to java.lang.String binding the statement
Hi,
I'm receiving a JSON with the following format:
{"TICKET_ID":"1","ACCOUNT_ID":null,"TICKET_REF":null, ..a lot of stuff here.......}
but when I call jsonModel.save(). I'm getting the following exception:
02-23 16:51:06.085 7205-7536/com.glownet.app.staging.debug D/Retrofit﹕ java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to java.lang.String
at com.glownet.app.glownet.entities.Admission$Container.bindToStatement(Admission$Container.java:46)
at com.glownet.app.glownet.entities.Admission$Container.bindToStatement(Admission$Container.java:16)
at com.raizlabs.android.dbflow.sql.SqlUtils.insert(SqlUtils.java:378)
at com.raizlabs.android.dbflow.sql.SqlUtils.save(SqlUtils.java:316)
at com.raizlabs.android.dbflow.structure.container.ContainerAdapter.save(ContainerAdapter.java:36)
at com.raizlabs.android.dbflow.structure.container.BaseModelContainer.save(BaseModelContainer.java:126)
Because in the Model$Cointainer the following check is created:
if (((java.lang.String)modelContainer.getValue("TICKET_REF")) != null) {
statement.bindString(1,((java.lang.String)modelContainer.getValue("TICKET_REF")));
A JSONObject.NULL is being tried to be converted to String with the cast.
How should I proceed?
The ticket ref is not a PK.
can you send me an example of the Model class? thanks
Sure
@Table(databaseName = AppDatabase.NAME)
@ContainerAdapter
public class Admission extends BaseModel {
@ContainerKey("TICKET_ID")
@Column(columnType = Column.PRIMARY_KEY)
String mTicketId;
@ContainerKey("TICKET_REF")
@Column
String mTicketReference;
}
I have more fields but all of them are strings or so.
Its a bug. Good find. Best bet until I release 1.6.0 (probably tomorrow) is to subclass JSONModel
and override this way:
@Override
public Object getValue(String columnName) {
Object value = getData() != null ? getData().opt(columnName) : null;
if(JSONObject.NULL.equals(value)) {
value = null;
}
return value;
}
JSONObject wraps null values in a NULL object.
Great! Thanks!
version 1.6.0 is now out