Very simple JSONObject unreported exception org.json.JSONException at design time

james pearce

This is very simple: Using Android Studio, I can't build a project because I'm getting the error:

Error: java: unreported exception org.json.JSONException; must be caught or declared to be thrown

Here is the offending code:

public static JSONObject toJsonObject(String json){
    JSONObject jsonObj = new JSONObject(json);
    return jsonObj;
}

I'm importing org.json.JSONObject; by the way.

The squiggly red line is underneath "new JSONObject(json)". It doesn't seem to like being passed a String, but everywhere I've seen e.g. https://github.com/douglascrockford/JSON-java/blob/master/JSONObject.java suggests it can.

I have tried making the class and method non-static, and have rebuilt projhect/restarted Android Studio.

I know this must be something simple I've missed...

0101100101

You pretty much always have to wrap all JSONObject and JSONArray usages in try-catch-blocks catching JSONException. In most cases it's boilerplate code that won't ever run. In the case of using new JSONObject(...) and new JSONArray(...), though, the try-catch-block is vital: It throws an exception if the passed String is not a valid JSON string. Make sure you always handle it somehow. In your case I would recommend this (where TAG is the name of your class):

public static JSONObject toJsonObject(String json){
    try {
        return new JSONObject(json);
    } catch (JSONException e) {
        Log.e(TAG, "Invalid JSON string: " + json, e);
        return null;
    }
}

If you don't put a try-catch-block in your toJsonObject(...)-method and instead add "throws JSONException" to the method declaration (which would actually solve the error), the full method would become completely redundant, by the way, because it doesn't do anything the JSONObject constructor doesn't.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

org.json.JSONException: JSONArray[0] is not a JSONObject - Java

From Dev

unhandled exception org.json.jsonexception

From Dev

org.json unreported exception while creating a JSONObject

From Dev

Android(Unhandled exception: org.json.JSONException)

From Dev

org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

From Dev

org.json.JSONException: Value <div of type java.lang.String cannot be converted to JSONObject during the debugging

From Dev

Android Unhandled exception: org.json.JSONException with ion

From Dev

POST Request to sendEmail endpoint causes org.json.JSONException: JSONObject["oobCode"] not found. after calling gitkitClient.getOobResponse(request);

From Dev

org.json.JSONException: JSONObject["ListeCar"] not found

From Dev

"org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject"

From Dev

org.json.JSONException: Value < of type java.lang.String cannot be converted to JSONObject

From Dev

Error parsing data org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject

From Dev

org.json.simple.jsonObject throws noclassdeffounderror proguard maven

From Dev

Very simple JSONObject unreported exception org.json.JSONException at design time

From Dev

org.json.JSONException: Value [....] of type org.json.JSONArray cannot be converted to JSONObject

From Dev

error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

From Dev

org.json.simple.JSONObject not found

From Dev

unable to access rest CXF with org.json.simple.JSONObject

From Dev

org.json.JSONException: JSONArray[0] not a string Exception

From Dev

org.json.JSONException: JSONObject["ListeCar"] not found

From Dev

org.json.JSONException: No value for {...}

From Dev

ERROR: com.android.volley.ParseError: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

From Dev

Get Org.json.JSONException:Value array(2) of type java.lang.String can not be converted to JSONObject

From Dev

ERROR in org.json.JSONException: of type org.json.JSONArray cannot be converted to JSONObject

From Dev

org.json.JSONException: Value Data of type java.lang.String cannot be converted to JSONObject

From Dev

org.json.JSONException:Value[{}] of type org.json.JSONArray cannot be converted to JSONObject

From Dev

org.json.JSONException: No value for data (for a simple app with a button)

From Dev

Android JsonObject Error org.json.JSONException: No value

From Dev

Encoding issue with org.json.simple.JSONObject - turning ★ to ★

Related Related

  1. 1

    org.json.JSONException: JSONArray[0] is not a JSONObject - Java

  2. 2

    unhandled exception org.json.jsonexception

  3. 3

    org.json unreported exception while creating a JSONObject

  4. 4

    Android(Unhandled exception: org.json.JSONException)

  5. 5

    org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

  6. 6

    org.json.JSONException: Value <div of type java.lang.String cannot be converted to JSONObject during the debugging

  7. 7

    Android Unhandled exception: org.json.JSONException with ion

  8. 8

    POST Request to sendEmail endpoint causes org.json.JSONException: JSONObject["oobCode"] not found. after calling gitkitClient.getOobResponse(request);

  9. 9

    org.json.JSONException: JSONObject["ListeCar"] not found

  10. 10

    "org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject"

  11. 11

    org.json.JSONException: Value < of type java.lang.String cannot be converted to JSONObject

  12. 12

    Error parsing data org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject

  13. 13

    org.json.simple.jsonObject throws noclassdeffounderror proguard maven

  14. 14

    Very simple JSONObject unreported exception org.json.JSONException at design time

  15. 15

    org.json.JSONException: Value [....] of type org.json.JSONArray cannot be converted to JSONObject

  16. 16

    error. org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

  17. 17

    org.json.simple.JSONObject not found

  18. 18

    unable to access rest CXF with org.json.simple.JSONObject

  19. 19

    org.json.JSONException: JSONArray[0] not a string Exception

  20. 20

    org.json.JSONException: JSONObject["ListeCar"] not found

  21. 21

    org.json.JSONException: No value for {...}

  22. 22

    ERROR: com.android.volley.ParseError: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

  23. 23

    Get Org.json.JSONException:Value array(2) of type java.lang.String can not be converted to JSONObject

  24. 24

    ERROR in org.json.JSONException: of type org.json.JSONArray cannot be converted to JSONObject

  25. 25

    org.json.JSONException: Value Data of type java.lang.String cannot be converted to JSONObject

  26. 26

    org.json.JSONException:Value[{}] of type org.json.JSONArray cannot be converted to JSONObject

  27. 27

    org.json.JSONException: No value for data (for a simple app with a button)

  28. 28

    Android JsonObject Error org.json.JSONException: No value

  29. 29

    Encoding issue with org.json.simple.JSONObject - turning ★ to ★

HotTag

Archive