Search code examples
javaarraysjsonspringjson-simple

Null pointer observed while trying to fetch the object value from JSON array


I'm trying to loop the calls: JSON array and trying to fetch the machine details JSON object which is present under calls JSON array list as like below:

{
   "<dynamicValue>":{
      "type":"CORR-ID",
      "tags":[
         {
            "name":"9VB6454145983212H",
            "flags":[
               "FLAG_DYNAMIC_VALUE",
               "FLAG_ID_LOOKUP_SUPPORTED"
            ]
         }
      ],
      "callSummary":[
         {
            "colo":"lvs",
            "pool":"amazon_paymentsplatformserv",
            "machine":"stage2utb29958"
         },
         {
            "colo":"lvs",
            "pool":"amazon_elmoserv",
            "machine":"msmamoserv_0"

         },
         {
            "colo":"lvs",
            "pool":"amazon_xopaymentgatewayserv",
            "machine":"msmastmentgatewayserv_1"
         },
         {
            "colo":"lvs",
            "pool":"amazon_paymentapiplatserv",
            "machine":"msmaentapiplatserv_2"
         },
         {
            "colo":"lvs",
            "pool":"amazon_userlifecycleserv_ca",
            "machine":"stage2utb91581"
         },
         {
            "colo":"lvs",
            "pool":"amazon_dafproxyserv",
            "machine":"msmasfproxyserv_1"
         },
         {
            "colo":"lvs",
            "pool":"paymentserv",
            "machine":"te-alm-15757_paymentexecutionserv_0",
            "calls":[
               {
                  "colo":"lvs",
                  "pool":"fimanagementserv_ca",
                  "machine":"msmgementserv_ca_20"
               },
               {
                  "colo":"lvs",
                  "pool":"fimanagementserv_ca",
                  "machine":"msmasgementserv_ca_4"
               }
               ]
              }
            ]
    }
}

The above JSON file which I stored in String variable and trying to fetch the machine details which is under calls: JSON ARRAY by using below code.

Code:

    public static void getHttpUrlformachineList(String response, String CalId, String componentName)
            throws Exception
      {
           //System.out.println(response);
           Map<String, String> data = new HashMap<String, String>();
           JSONParser parser = new JSONParser();
           JSONObject object = (JSONObject) parser.parse(response);
           JSONObject getValue = (JSONObject) object.get(CalId.trim()); //CalId is the dynamic value that mentioned in the JSON input file
           JSONObject getCalSummary = (JSONObject) object.get("callSummary");
           JSONArray arrays=(JSONArray) getCalSummary.get("calls");
           System.out.println(arrays.size()); // return null pointer
       }

Error:

    java.lang.NullPointerException: null
        at com.online.amazon.hadoop.cal.swagger.utils.Utils.getHttpUrlformachineList(Utils.java:112) ~[classes/:na]

If you notice that calls Array List will not be available in all the callSummary JSON Array, and It will be dynamic and can be available under any component that listed above.

So I just want to dynamically get the calls: JSON array and iterate and fetch machine details.

Can someone help me to achieve this?

Note: I'm using JSON-Simple library to parse and iterate the JSON. It would be great if I get solution on the same.

Updated:

I also tried to create callSummary as JSON array and loop that array to get each JSON object and tried to find the calls but this is also leads to Null pointer.

Also the calls json array is not index specific. It can be anywhere in the payload. It may or may not be there in the payload. I just need to handle if it's exist in any of the component then I need to fetch that machine details


Solution

  • You should match the types as specified in your JSON string:

     public static void getHttpUrlformachineList(String response, String CalId, String componentName)
                throws Exception
          {
               //System.out.println(response);
               Map<String, String> data = new HashMap<String, String>();
               JSONParser parser = new JSONParser();
               JSONObject object = (JSONObject) parser.parse(response);
               JSONObject getValue = (JSONObject) object.get(CalId.trim()); //CalId is the dynamic value that mentioned in the JSON input file
               JSONArray getCalSummary = (JSONArray) object.get("callSummary"); // callSummary is a JSONArray, not JSONObject
               for (int i = 0; i < getCalSummary.length(); i++) {
                   JSONObject obj = getCalSummary.getJSONObject(i);
                   if (obj.has("calls")) {
                       // grab calls array:
                       JSONArray callsArray = obj.getJSONArray("calls");
                   }
               }
           }
    

    Here, you should also check your JSON values with .has(...) method to avoid getting JSONException if a field doesn't exists in your JSONObject.