question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to java.lang.String binding the statement

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
agrosnercommented, Feb 24, 2015

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.

0reactions
agrosnercommented, Feb 24, 2015

version 1.6.0 is now out

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.ClassCastException: org.json.JSONObject$1 cannot ...
org.json.JSONObject$1 cannot be cast to org.json.JSONObject seems that object is of type JSONObject$1 and its refer this object to JSONObject..
Read more >
java.lang.String cannot be cast to org.json.simple.JSONObject
Solved: I have a requirement to generate a RITM by copying Variables into new RITM. var gr = new GlideRecord("sc_req_item"); if.
Read more >
ClassCastException "Java.Lang.String Cannot Be Cast To ...
Oracle JDeveloper - Version 12.2.1.2.0 and later: ClassCastException "Java.Lang.String Cannot Be Cast To " When Using af:dynamicComponent.
Read more >
"org.json.simple.JSONObject cannot be cast to java ... - ERROR
ERROR: "org.json.simple.JSONObject cannot be cast to java.lang.String error" while importing a cluster configuration in Administrator Console ...
Read more >
openHAB Cloud "class org.json.JSONObject cannot be cast to ...
2022-08-08 14:47:31.816 [ERROR] [io.socket.thread.EventThread ] - Task threw exception java.lang.ClassCastException: class org.json.JSONObject ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found