android developer
android developer

Reputation: 43

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 error

Can anyone help me? I debugg app many more time but I can not found what exact issue going in my android app. In my android app, My cases are describe below. cases 1: Insert data into database It is perfectly worked fine. case 2: That data display in to recycler view ok. It will display fine.

It will work with only single user login. When another user will login in to app and try to add data then It will get this exception. Or when I will refresh first user login view at that time it have same exception.

Or when I remove second user from database then I refresh first login user view at that time it will work perfectly . Now what to do? Please any one help me out of this issue. I attached screen shot of my error log please refer it.

Retrive data.java

 private void makeJsonArrayRequest(final String email) {
    String cancel_req_tag = "list";

    JsonArrayRequest req = new JsonArrayRequest(URL_FOR_SELECT,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d("OnResponse", response.toString());

                    try {
                        // Parsing json array response
                        // loop through each json object

                        for (int i = 0; i < response.length(); i++) {

                            JSONObject jsonObject = response.getJSONObject(i);
                            ListMobileModal objMobileModal = new ListMobileModal();
                            if (jsonObject.getString("email").equals(email)) {
                                objMobileModal.email = jsonObject.getString("email");
                                if (!jsonObject.isNull("name")) {
                                    objMobileModal.lname = jsonObject.getString("name"); //here we can fetch webservice field
                                }
                                if (!jsonObject.isNull("title")) {
                                    objMobileModal.title = jsonObject.getString("title"); //here we can fetch webservice field
                                }
                                if (!jsonObject.isNull("eimage")) {
                                    objMobileModal.eimage = jsonObject.getString("eimage");
                                }
                                if (!jsonObject.isNull("eid")) {
                                    objMobileModal.eid = jsonObject.getString("eid");
                                }
                                if (!jsonObject.isNull("ename")) {
                                    objMobileModal.ename = jsonObject.getString("ename");
                                }
                                if (!jsonObject.isNull("edescription")) {
                                    objMobileModal.edescription = jsonObject.getString("edescription");
                                }
                                if (!jsonObject.isNull("evlocation")) {
                                    objMobileModal.elocation = jsonObject.getString("elocation");
                                }

                                lstListMobile.add(i, objMobileModal);
                            }
                        }

                        objMobileAdapter = new ListMobileAdapter(SavedPhoneBottomActivity.this, lstListMobile);
                        objEmptyRecyclerViewAdapter = new EmptyRecyclerViewAdapter("No selected list!");
                        if (lstListMobile == null || lstListMobile.size() == 0) {
                            rvDisplay.setAdapter(objEmptyRecyclerViewAdapter);
                            rvDisplay.setVisibility(View.VISIBLE);


                        } else {
                            rvDisplay.setAdapter(objMobileAdapter);
                            rvDisplay.setVisibility(View.VISIBLE);

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(SavedPhoneBottomActivity.this,
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("VolleyError", "Error: " + error.getMessage());
            Toast.makeText(SavedPhoneBottomActivity.this,
                    error.getMessage(), Toast.LENGTH_SHORT).show();
        }
       });
           AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(req, cancel_req_tag);
}

List exception error

Upvotes: 0

Views: 1027

Answers (3)

user1626594
user1626594

Reputation:

You're inserting lstListMobile.add(i, objMobileModal); maybe you can just add lstListMobile.add(objMobileModal);? Probably whats happening is that when i==0 is does not pass the test if (jsonObject.getString("email").equals(email)) {. So it does not add and the size remains 0. When i==1 it breaks when inserting.

Upvotes: 0

Meenakshi Mishra
Meenakshi Mishra

Reputation: 66

try to replace lstListMobile.add(i, objMobileModal); with lstListMobile.add(objMobileModal);

when if (jsonObject.getString("email").equals(email)) condition fails 0 index is not added for that user.

you will get ArrayIndexOutOfBound error on lstListMobile.add(1, objMobileModal); if 0 index is not present

Upvotes: 1

Ratilal Chopda
Ratilal Chopda

Reputation: 4220

Please check first array is not null

if (response.length > 0) {
    // do something         
    }
else{
     //empty array
  }

Upvotes: 0

Related Questions