Skip to content Skip to sidebar Skip to footer

How To Solve Org.json.jsonexception: Index 0 Out Of Range [0..0) Using Android

I am developing an app, here i want to display fetched items of table I tried following code but it gives error at this line JSONObject c = result.getJSONObject(0);. How do i solve

Solution 1:

Check the size of the result array before you iterate over it.

@Override
protected void onPostExecute(JSONObject json) {
{
 pDialog.dismiss();
 try {
  // Getting JSON Array
  result = json.getJSONArray(TAG_USER);
  if (result.length() > 0) {
   JSONObject c = result.getJSONObject(0);

   // Storing  JSON item in a Variable
   //    String id = c.getString(TAG_ID);
   String name = c.getString(TAG_NAME);
   String amount = c.getString(TAG_AMOUNT);

   //Set JSON Data in TextView
   //    uid.setText(id);
   name1.setText(name);
   amount1.setText(amount);
  }

 } catch (JSONException e) {
  e.printStackTrace();
 }
}

Post a Comment for "How To Solve Org.json.jsonexception: Index 0 Out Of Range [0..0) Using Android"