0

My JSON is like following :

[
    {
        "start_time": "06:00:00",
        "end_time": "21:00:00",
        "id": 1845,
        "number": "",
        "name": "Default Store",
        "companyId": 731,
        "address": null,
        "phone1": null,
        "phone2": null,
        "fax1": null,
        "fax2": null,
        "city": "Abbeville",
        "zipcode": null,
        "time_zone": "America/New_York",
        "meal_break_length": null,
        "consecutive_minutes": null,
        "minHoursBetweenShifts": null,
        "task_delay": null,
        "maxShiftsPerDay": null,
        "start_day": 1,
        "notes": null,
        "isActive": 1,
        "countryId": 184,
        "state": "AL",
        "rsstoreid": null,
        "use_override": null,
        "forecast_override_cashier": null,
        "forecast_override_customer_coordinator": null,
        "forecast_override_sales_associate": null,
        "vendorId": null,
        "fdId": null,
        "dashboardPin": "D3FFS6RW",
        "dropSwapDeadlineHours": null,
        "openShiftDeadlineHours": 24,
        "country": "US"
    },
    {
        "start_time": "06:00:00",
        "end_time": "21:00:00",
        "id": 1846,
        "number": "",
        "name": "Testing Store 001",
        "companyId": 731,
        "address": null,
        "phone1": null,
        "phone2": null,
        "fax1": null,
        "fax2": null,
        "city": "Abbeville",
        "zipcode": null,
        "time_zone": "America/New_York",
        "meal_break_length": null,
        "consecutive_minutes": null,
        "minHoursBetweenShifts": null,
        "task_delay": null,
        "maxShiftsPerDay": null,
        "start_day": 1,
        "notes": null,
        "isActive": 1,
        "countryId": 184,
        "state": "AL",
        "rsstoreid": null,
        "use_override": null,
        "forecast_override_cashier": null,
        "forecast_override_customer_coordinator": null,
        "forecast_override_sales_associate": null,
        "vendorId": null,
        "fdId": null,
        "dashboardPin": "A33FUNJL",
        "dropSwapDeadlineHours": null,
        "openShiftDeadlineHours": 24,
        "country": "US"
    },
    {
        "start_time": "06:00:00",
        "end_time": "21:00:00",
        "id": 1847,
        "number": "",
        "name": "Testing Store 002",
        "companyId": 731,
        "address": null,
        "phone1": null,
        "phone2": null,
        "fax1": null,
        "fax2": null,
        "city": "Abbeville",
        "zipcode": null,
        "time_zone": "America/New_York",
        "meal_break_length": null,
        "consecutive_minutes": null,
        "minHoursBetweenShifts": null,
        "task_delay": null,
        "maxShiftsPerDay": null,
        "start_day": 1,
        "notes": null,
        "isActive": 1,
        "countryId": 184,
        "state": "AL",
        "rsstoreid": null,
        "use_override": null,
        "forecast_override_cashier": null,
        "forecast_override_customer_coordinator": null,
        "forecast_override_sales_associate": null,
        "vendorId": null,
        "fdId": null,
        "dashboardPin": "5KMKTJ7M",
        "dropSwapDeadlineHours": null,
        "openShiftDeadlineHours": 24,
        "country": "US"
    }
]

Now from this array, there are objects, which have key of 'id', I need to get all these ID's, like:

[1845, 1846, 1847]

I am using following technique:

private static JsonNode getResponseBody(String baseURL, String overTimeRulesPath) throws IOException {
    String url = baseURL + overTimeRulesPath;
    String token = getToken("username", "password");
    HttpResponse<JsonNode> endPointResponse;
    try{
        endPointResponse = Unirest.get(url).header("x-access-token", token).asJson();
    }catch (UnirestException e) {
        throw new IOException(e);
    }
    return endPointResponse.getBody();
}

public static JSONArray getStores() throws IOException {
    int companyID = getCompanyID("username", "password");
    String baseURL = "baseURL";
    String storesPath = "company/"+companyID+"/Stores";
    JSONObject responseJson = getResponseBody(baseURL, storesPath).getObject();
    return responseJson.getJSONArray("id");
}

and I am getting nullPointerException at my return line, which is return responseJson.getJSONArray("id"); on that line.

What possibly, could have gone wrong.

Taimoor Pasha
  • 192
  • 2
  • 3
  • 16

0 Answers0