Shayma
Shayma

Reputation: 59

How to fix Error json.JSONException: Index 1 out of range [0..1)

My project get bus ticket time information from api. But it don't show going time if there is no data for return time from server. This is the error message when I send request by Volley.

Error json.JSONException: Index 1 out of range [0..1)

code snippet

private void sendRequest(final String owner, final Map<String, String> header) {
    StringRequest stringRequest = new StringRequest(Request.Method.POST, MyConstants.URL + owner,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                 //   Log.e("AAAA" + owner, response);
                    try {
                        JSONObject object = new JSONObject(response);
                        if (object.getString(MyConstants.SERVICE_STATUS).equals(MyConstants.SERVICE_RESPONSE_STATUS_NOTAVAILABLE)) {
                            // servisten gelen cevap not_available ise
                            //// owner
                            sendVoyagesErrorBroadcast(owner, MyConstants.ERROR_NOTAVAILABLE);
                        } else if (object.getString(MyConstants.SERVICE_STATUS).equals(MyConstants.SERVICE_RESPONSE_STATUS_SUCCESS)) {
                            JSONArray result = object.getJSONArray(MyConstants.SERVICE_RESULT);
                            JSONArray resultGoing = result.getJSONObject(0).getJSONArray("going");
                            if (has_return) {
                                JSONArray resultReturn = result.getJSONObject(1).getJSONArray("round");
                                sendVoyagesArrayBroadcast(owner + MyConstants.DIRECTION_RETURN, resultReturn);
                            }
                            sendVoyagesArrayBroadcast(owner + MyConstants.DIRECTION_GOING, resultGoing);
                        } else if (object.getString(MyConstants.SERVICE_STATUS).equals(MyConstants.SERVICE_RESPONSE_STATUS_FAİLURE)) {
                            sendVoyagesErrorBroadcast(owner, MyConstants.ERROR_SERVER);
                        }
                    } catch (JSONException e) {
                        Log.e("search" + owner + "VoyagesErr1", e.toString());
                        e.printStackTrace();
                    }
                }

Upvotes: 1

Views: 1071

Answers (1)

Ravi
Ravi

Reputation: 729

Please check the following code

 if (has_return) {
     JSONArray resultReturn = result.getJSONObject(1).getJSONArray("round");
     sendVoyagesArrayBroadcast(owner + MyConstants.DIRECTION_RETURN, resultReturn);
  }

You are trying to access the element at index 1, which is probably not present. Index starts from 0 not 1 and hence even if the result json array size is 1 it will give an error

try to do something like this

if (has_return) {
    if (result.length() > 1)
        JSONArray resultReturn = result.getJSONObject(1).getJSONArray("round");
        sendVoyagesArrayBroadcast(owner + MyConstants.DIRECTION_RETURN, resultReturn);
    }
}

or else if you are interested in first element then access it like

if (result.length() > 0) {
    JSONArray resultReturn = result.getJSONObject(0).getJSONArray("round");
}

Upvotes: 1

Related Questions