user9802288
user9802288

Reputation:

Excepton java.lang.IndexOutOfBoundsException

I get exception java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 for the below code. But couldn't understand why?

String zTab = mNames.get(0);
Toast.makeText(context, zTab, Toast.LENGTH_LONG).show();`

JSON

{"nazwa":"Rower"},,{"nazwa":"Rower mtb"},{"nazwa":"rower"}

Upvotes: 0

Views: 434

Answers (2)

Hamza
Hamza

Reputation: 251

// in your code
 JSONArray jsonArray = new JSONArray(result);
            for(int i = 0; i<jsonArray.length(); i++)
            {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
            // make change in this below line

                mNames.add(jsonObject.optString("nazwa"));
            }
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

Upvotes: 1

Gent Ahmeti
Gent Ahmeti

Reputation: 1599

The List mNames doesn't have any items on it. If you call mNames.size() it would return 0 or mNames.isEmpty() will return true.

Make sure you fill your List before querying for items inside.

Upvotes: 2

Related Questions