3

At the outset, sorry about not being able to provide my code. I created a JSONObject and I am converting to string and my Android app crashes sometimes with the following error.

E/AndroidRuntime( 6162): java.util.ConcurrentModificationException
E/AndroidRuntime( 6162):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
E/AndroidRuntime( 6162):    at org.json.JSONArray.writeTo(JSONArray.java:612)
E/AndroidRuntime( 6162):    at org.json.JSONStringer.value(JSONStringer.java:233)
E/AndroidRuntime( 6162):    at org.json.JSONObject.writeTo(JSONObject.java:720)
E/AndroidRuntime( 6162):    at org.json.JSONObject.toString(JSONObject.java:689)

From my research, I am not able to tell why there's a ConcurrentModificationException. I have only one thread accessing the information. I don't think the toString() does any removal. Can someone explain if they have seen any such issue in Android and how they might have gone about sorting it?

EDITED

Added a slightly modified code per @domi's suggestion. The problem is consistently seen in the line

message.put("message", tempObject.toString());

private void message() {
    if (this.jsonObject == null) 
        this.jsonObject = new JSONObject();
    try {
        TimeZone tz = TimeZone.getTimeZone("UTC");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'", Locale.US);
        df.setTimeZone(tz);
        Date date = new Date();
        String dateString = df.format(date);
        System.out.println(dateString);
        jsonObject.put("Date", dateString);

        JSONObject jsObj = new JSONObject();
        jsObj.put("Cat", Cat.getInfo());
        jsonObject.put("Cat", jsObj);

        JSONArray tempInfo = getJsonArray(Dog.getInfo());

        jsonObject.put("Dog", tempInfo);
    } catch (JSONException e) {
        e.printStackTrace();
    }


    TimeZone tz = TimeZone.getTimeZone("UTC");
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'", Locale.US);
    df.setTimeZone(tz);
    Date date = new Date();
    String dateString = df.format(date);

    if (this.message == null)
        this.message = new JSONObject();

    try {

        message.put("topic", topic);
        JSONObject tempObject = new JSONObject();

        synchronized(this.jsonObject) {
            System.out.println("In lock" + dateString);
            tempObject = this.jsonObject;
            this.jsonObject = null;
        }

        message.put("message", tempObject.toString());
        tempObject = null;

        synchronized (this.message) {
            doSomething(message);
        }
        System.out.println("Out lock");
    } catch (JSONException e) {
        e.printStackTrace();
    } finally {
        System.out.println(message);
    }
}

1 Answers1

2

I was able to resolve this. The problem was

message.put("message", tempObject.toString());

The method call tempObject.toString() leads to an iteration through the list (presumption) and the message.put(...) leads to accessing the resultant string. Sometimes this leads to ConcurrentModificationException. I was able to resolve it by doing it in two lines and having some locks in place.