0

I am working on Coverage area for restaurants where I need to display message if the selected restaurant is not in coverage area. If it is in coverage area, the user can proceed to order the meal.

As far as there is restaurant is not null means if the restaurant is in coverage area, the app is running perfectly. When there is no coverage, say the JSON Array is null then the above mentioned error is displaying in catch block.

Here is the code:

`public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    progressActivity.showContent();
                    JSONObject[] restaurants_arr = null;

                    hasCoverage = true;
                    try {
                        if (response != null)
                        {

                            JSONArray restaurants = response.getJSONArray("restaurants");

                            // if restaurants deliver to that area
                            if (restaurants.length() > 0 && restaurants.getJSONObject(0) != null)
                            {

                                restaurants_arr = new JSONObject[restaurants.length()];

                                int has_coverage = 1; // assume coverage is there..

                                for (int j = 0; j < Utils.cart_restaurants.size(); j++)
                                {
                                    RestaurantModel restaurant = Utils.cart_restaurants.get(j);
                                    String restaurantCartID = restaurant.getId();

                                    boolean found = false;
                                    for (int i = 0; i < restaurants.length(); i++) {
                                        restaurants_arr[i] = restaurants.getJSONObject(i);

                                        String restaurantID = restaurants_arr[i].get("restaurant_id").toString();

                                        if (restaurantCartID.equals(restaurantID))
                                        {
                                            found = true;
                                            break;
                                        }
                                    }  //end of inner for
                                    if (found == false)
                                    {
                                        Toast.makeText(CheckoutActivity.this, "There is no coverage for the Selected Area ", Toast.LENGTH_SHORT).show();
                                        hasCoverage = false;
                                        break;
                                    }

                                }
                                //end of outer for
                            } //end of if

                            else //if restaurants don't deliver to that area
                            {
                                hasCoverage = false;
                            }

                        } // end of if response != null
                        if(hasCoverage)
                        {
                            processOrder();
                        }
                        else
                        {
                            Toast.makeText(CheckoutActivity.this, "There is no coverage for the Selected Area ", Toast.LENGTH_SHORT).show();
                        }
                    } // end of try
                    catch (Exception ex) {
                        GSLogger.e(ex);
                        showError();
                    }
                }

`

When i set debug point, It not allowing me to get inside of if (restaurants.length() > 0 && restaurants.getJSONObject(0) != null) { this line.

2 Answers 2

1

This is the Only change I made. Now It is working perfectly. if (!restaurants.getString(0).equals("null"))

Sign up to request clarification or add additional context in comments.

Comments

0

Apply a check to see if restaurant null

if (restaurant !=null && restaurants.length() > 0 && restaurants.getJSONObject(0) != null)

2 Comments

same error. JSONArray is null. I want to display toast message rather than going into try catch block
print restaurants or toggle a breakpoint to see its value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.