0

I am parsing JSON that is coming back from REST callout, and when running, it is throwing this error: attemp to de-reference null object. The issue seems to be with how I am parsing the JSON, but I am not sure where I am going wrong. Can someone help?

Here is the snippet of the JSON I am parsing (bold parts are those that are causing the error):

"result": {
"first_name": "JOHN",
"last_name": "SMITH",
"id_type": "MI",
"subscriber_id": "1234567",
"gender": "M",
"birthdate": "19601119",
"address": {
"info": "123 Main St.",
"city": "New York",
"state": "NY",
"zip_code": "100000"
},
**"county:": {
"value": "14"
},
"office": {
"value": "H78"
},
"recert_month": {
"value": "12",
"name": "December"
},**
"dates": [
{
"name": "Service",
"qualifier": "472",
"date": "2020-09-29"
},
{
"name": "Plan Begin",
"qualifier": "346",
"date": "2020-09-01"
},
{
"name": "Issue",
"qualifier": "102",
"date": "2020-06-01"
}
],

Here is how I am parsing it (bold part is causing error):

    Map<String, Object> wholeResponse = (Map<String, Object>) JSON.deserializeUntyped(response2.getBody());
        Map<String, Object> result = (Map<String, Object>) wholeResponse.get('result');
            Map<String, Object> residentAddress = (Map<String, Object>) result.get('address');
               **Map<String, Object> county = (Map<String, Object>) result.get('county');
              Map<String, Object> office = (Map<String, Object>) result.get('office');
             Map<String, Object> recert_month = (Map<String, Object>) result.get('recert_month');**
    
   Eligibility__c newEligRec = new Eligibility__c(); 
    newEligRec.Referral_Name__c = ref.id;
    newEligRec.First_Name__c = string.valueof(result.get('first_name'));
    newEligRec.Last_Name__c = string.valueof(result.get('last_name'));
    newEligRec.Name_Id_Type__c = string.valueof(result.get('id_type'));
    newEligRec.Client_ID__c = string.valueOf(result.get('subscriber_id'));
    newEligRec.Gender__c = string.valueOf(result.get('gender'));
    string birthdate;
          if (string.valueof(result.get('birthdate')) != null){
              birthdate = string.valueof(result.get('birthdate')).remove('-');
              string year = birthdate.substring(0,4);
              string month = birthdate.substring(4,6);
              string day = birthdate.substring(6,8);
             birthdate = month + '/' + day + '/' + year;
                }
    newEligRec.Date_of_Birth__c = birthdate;
    newEligRec.Address_Number_and_Street__c = string.valueOf(residentAddress.get('info')); 
    newEligRec.City__c =  string.valueOf(residentAddress.get('city'));    
    newEligRec.State__c =  string.valueOf(residentAddress.get('state'));    
    newEligRec.Zip_Code__c = string.valueOf(residentAddress.get('zip_code'));
    **newEligRec.County__c = string.valueOf(county.get('value'));
    newEligRec.Office__c = string.valueOf(office.get('value'));
    newEligRec.Recertification_Month_value__c = string.valueOf(recert_month.get('value'));
    newEligRec.Recertification_Month__c = string.valueOf(recert_month.get('name'));**
5
  • That line cannot throw a null pointer exception. Your problem is on a different line.
    – sfdcfox
    Sep 30, 2020 at 14:21
  • The lines that are throwing the null pointer exception are the last 4 lines (when I comment each one out individually, each of the others throw a null pointer exception.) I just meant to highlight the part of the JSON that I am having trouble with. newEligRec.County__c = string.valueOf(county.get('value')); newEligRec.Office__c = string.valueOf(office.get('value')); newEligRec.Recertification_Month_value__c = string.valueOf(recert_month.get('value')); newEligRec.Recertification_Month__c = string.valueOf(recert_month.get('name'));
    – Michael M
    Sep 30, 2020 at 14:33
  • Have you checked out our canonical QA, How do I get started working with JSON in Apex?? This JSON looks amenable to parsing with a wrapper class.
    – David Reed
    Sep 30, 2020 at 16:36
  • Are you certain that each message you receive contains the keys county, office, and recert_month?
    – David Reed
    Sep 30, 2020 at 16:39
  • I am certain it has county, but not necessarily office and recert_month. What would you recommend adding to the code to handle scenarios where it does not contain those keys? a try/catch block?
    – Michael M
    Sep 30, 2020 at 16:44

1 Answer 1

0

These four lines are vulnerable to NullPointerException:

newEligRec.County__c = string.valueOf(county.get('value'));
newEligRec.Office__c = string.valueOf(office.get('value'));
newEligRec.Recertification_Month_value__c = string.valueOf(recert_month.get('value'));
newEligRec.Recertification_Month__c = string.valueOf(recert_month.get('name'));

If county, office, or recert_month is not present in the incoming message, each of these lines can throw a NullPointerException because, e.g.,

Map<String, Object> county = (Map<String, Object>) result.get('county');

is null.

You'll need to write conditional logic to check if these variables are null before you attempt to access their properties with get(), and presumably provide default values or decline to populate the linked field.

7
  • Ok- thank you!! I have another question re parsing this JSON- it is a basic question. Should I ask it here? My question is how to parse this section: "dates": [ { "name": "Service", "qualifier": "472", "date": "2020-09-29" }, { "name": "Plan Begin", "qualifier": "346", "date": "2020-09-01" }, { "name": "Issue", "qualifier": "102", "date": "2020-06-01" } ], and be able to get the string value of each one.. The first step is this: List<Object> dates = (List<Object>) result.get('dates'); but then how do I access each of the 3 objects in that list and their map key values?
    – Michael M
    Sep 30, 2020 at 17:07
  • I recommend looking at the canonical QA I linked above. I think it covers this area.
    – David Reed
    Sep 30, 2020 at 17:18
  • It doesn't look like it covers this area...
    – Michael M
    Sep 30, 2020 at 17:45
  • Basically you just have to keep calling get() and casting - or, better, use a statically typed wrapper class to deserialize.
    – David Reed
    Sep 30, 2020 at 17:47
  • I am a newbie to this so am not getting this. Here is what I'm trying: List<Object> lstdates = (List<Object>) result.get('dates'); Map<String, Object> abc = new Map<String, Object>(); abc.add(lstdates[0]); but it won't let me save, saying: Method does not exist or incorrect signature: void add(Object) from the type Map. I'm stuck on what to do, specifically, after this step "List<Object> lstdates = (List<Object>) result.get('dates');" esp. because each object within the "dates" list have same keys, so don't know how to differentiate with get()
    – Michael M
    Sep 30, 2020 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .