-1

I am getting this above error when the json response values are null

{
"restaurant_area_min_order":null,
"restaurant_id":null
}.

The above error is appearing inside try catch block. When there is values inside restaurant_area_min_order and restaurant_id, The function is working fine without error. How can I check if the response data are null and continue the function without any error?

Here is my code.

private void getAreaMinOrders()
{
    for(int i = 0; i < Utils.cart_restaurants.size(); i++)
    {
        RestaurantModel restaurant = Utils.cart_restaurants.get(i);
        String restaurant_id = restaurant.getId();

        String url = LinksConstants.BASE_URL + LinksConstants.CHECK_MINIMUM_ORDER;

        RequestParams params = new RequestParams();
        params.put("restaurant_id", restaurant_id);
        params.put("area_id", city_id);

        NetworkRestClient.post(url, params, new JsonHttpResponseHandler()
        {
            @Override
            public void onStart()
            {
                super.onStart();
                progressActivity.showLoading();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response)
            {
                super.onSuccess(statusCode, headers, response);
                try
                {
                    if (response != null)
                    {
                        String restaurant_id = response.getString("restaurant_id");

                        String rest_area_min_order = null;

                        if(rest_area_min_order != null)
                        {
                            rest_area_min_order = response.getString("restaurant_area_min_order");
                        }
                        HashMap<String, Object> items_hash = (HashMap<String, Object>) rest_cart_hash.get(restaurant_id);
                        items_hash.put("rest_area_min_order", rest_area_min_order);
                    }
                    progressActivity.showContent();
                }
                catch (Exception ex)
                {
                    GSLogger.e(ex);
                    showError();
                }
            }


            @Override
            public void onFailure(int statusCode, Header[] headers, String errorResponse, Throwable throwable)
            {
                super.onFailure(statusCode, headers, errorResponse, throwable);
                showError();
                if (AppConstants.DEBUG_MODE)
                    showToast(errorResponse);
            }


        }); //end of NetworkRestClient.post
    } //end of for
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jacky
  • 298
  • 3
  • 15
  • 1
    If `restaurant_id` is null, what do you believe the value of `items_hash` will be? If null, then what do you think `items_hash.put()` will do? If you think it'll throw the exception in your title, you'd be right. --- On a side note: What is the purpose of `if(rest_area_min_order != null)` right after `String rest_area_min_order = null`? When would that `if` statement *ever* be true? – Andreas Aug 08 '17 at 16:12

1 Answers1

2

You can use JSONObject.isNull() method to check for null.

response.isNull("your_key"); // returns true if the value of that key is null

But from the details you have posted, it looks like the HashMap<String, Object> items_hash is null in your code.

Add null check:

if(restaurant_id != null) {
      HashMap<String, Object> items_hash = (HashMap<String, Object>) rest_cart_hash.get(restaurant_id);
      items_hash.put("rest_area_min_order", rest_area_min_order);
}
Bob
  • 13,447
  • 7
  • 35
  • 45