简体   繁体   中英

Value of null 0 java.lang.String cannot be converted to JSONObject [android]

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.

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

应用检查以查看餐厅是否为空

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM