0

How can I bypass an empty JSON value and give it a default value. This is my code:

private static Cursor extractFeatureFromJson(String foodJSON) {
    // If the JSON string is empty or null, then return early.
    if (TextUtils.isEmpty(foodJSON)) {
        return null;
    }

    try {

        JSONArray foodArray = new JSONArray(foodJSON);

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

            JSONObject foodObject = foodArray.getJSONObject(i);

            ContentValues values = new ContentValues();
            values.put(COLUMN_NDB_NO, foodObject.getInt(COLUMN_NDB_NO));
            values.put(COLUMN_NAME, foodObject.getString(COLUMN_NAME));
            values.put(COLUMN_WATER_G, foodObject.getInt(COLUMN_WATER_G));
            values.put(COLUMN_ENERGY_KCAL, foodObject.getInt(COLUMN_ENERGY_KCAL));
            values.put(COLUMN_PROTEIN_G, foodObject.getInt(COLUMN_PROTEIN_G));
            values.put(COLUMN_LIPID_TOT_G, foodObject.getInt(COLUMN_LIPID_TOT_G));
            values.put(COLUMN_ASH_G, foodObject.getInt(COLUMN_ASH_G));
            values.put(COLUMN_CARBOHYDRT_G, foodObject.getInt(COLUMN_CARBOHYDRT_G));

            foodNutriProvider insert = new foodNutriProvider();

            insert.insert(CONTENT_URI,values);

        }

    } catch (JSONException e) {

        Log.e("foodSearch", "Problem parsing the earthquake JSON results", e);
        Log.e("foodSearch", foodJSON);
    }

    foodNutriProvider getTable = new foodNutriProvider();

    Cursor foodTable = getTable.query(CONTENT_URI, null, null, null, null);

    return foodTable;
}

The app crashes whenever it faces an empty field and for my app it doesn't really matter if its empty and I can give it a 0 instead.

Zankrut Parmar
  • 1,872
  • 1
  • 13
  • 28
K.hamdan
  • 103
  • 1
  • 1
  • 9

3 Answers3

0

I'm assuming that your app crashes with NUll Pointer error. If so, you must do a check for null values by using if condition.

abhi
  • 1,412
  • 19
  • 25
0

For JSONObject :- foodJSONObject.isNull()

For JSONArray :- foodJSONArray.isNull(index)

For Getting values from a key :- foodJSONObject.optString("KEY_PARAMETER")

Rohit Singh
  • 403
  • 4
  • 9
  • if(foodObject.optString(COLUMN_WATER_G)==null){ values.put(COLUMN_WATER_G, 0); }else{ values.put(COLUMN_WATER_G, foodObject.getInt(COLUMN_WATER_G)); } this didnt work – K.hamdan Feb 11 '19 at 12:21
  • you need to use if(foodObject.optString(COLUMN_WATER).isNullorBlank()){ //****your code } – Rohit Singh Feb 12 '19 at 13:18
0

Instead of using getInt or getString with JSONObject , I will suggest you to use optInt or optString and it will automatically handle the null pointer exception for you and they are JSONObject internal method.

I have just replaced all getInt and getString with optInt and optString in your code, so request you to please check and let me know if this works for you.

Also, request you to please cross check the json data type you are trying to fetch from response. Like you are trying to fetch string and you are using getInt.

 private static Cursor extractFeatureFromJson(String foodJSON) {
        // If the JSON string is empty or null, then return early.
        if (TextUtils.isEmpty(foodJSON)) {
            return null;
        }

        try {

            JSONArray foodArray = new JSONArray(foodJSON);

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

                JSONObject foodObject = foodArray.getJSONObject(i);

                ContentValues values = new ContentValues();
                values.put(COLUMN_NDB_NO, foodObject.optInt(COLUMN_NDB_NO));
                values.put(COLUMN_NAME, foodObject.optString(COLUMN_NAME));
                values.put(COLUMN_WATER_G, foodObject.optInt(COLUMN_WATER_G));
                values.put(COLUMN_ENERGY_KCAL, foodObject.optInt(COLUMN_ENERGY_KCAL));
                values.put(COLUMN_PROTEIN_G, foodObject.optInt(COLUMN_PROTEIN_G));
                values.put(COLUMN_LIPID_TOT_G, foodObject.optInt(COLUMN_LIPID_TOT_G));
                values.put(COLUMN_ASH_G, foodObject.optInt(COLUMN_ASH_G));
                values.put(COLUMN_CARBOHYDRT_G, foodObject.optInt(COLUMN_CARBOHYDRT_G));

                foodNutriProvider insert = new foodNutriProvider();

                insert.insert(CONTENT_URI,values);

            }

        } catch (JSONException e) {

            Log.e("foodSearch", "Problem parsing the earthquake JSON results", e);
            Log.e("foodSearch", foodJSON);
        }

        foodNutriProvider getTable = new foodNutriProvider();

        Cursor foodTable = getTable.query(CONTENT_URI, null, null, null, null);

        return foodTable;
    }
Pankaj Mundra
  • 1,401
  • 9
  • 25