You need to sign in to do that
Don't have an account?
Michael M
JSON.deserializeUntyped issue: attempt to de-reference null object
Hello, 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'));
- Also, as a bonus, how would I go about parsing the "dates" list from the JSON? Thank you very much for any help you can provide.
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'));
- Also, as a bonus, how would I go about parsing the "dates" list from the JSON? Thank you very much for any help you can provide.
https://salesforce.stackexchange.com/questions/228538/json-content-display-attempt-to-de-reference-a-null-object
For further reference, you can check this too,
https://developer.salesforce.com/forums/?id=9060G0000005l0iQAA
I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.
Thanks.
All Answers
https://salesforce.stackexchange.com/questions/228538/json-content-display-attempt-to-de-reference-a-null-object
For further reference, you can check this too,
https://developer.salesforce.com/forums/?id=9060G0000005l0iQAA
I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.
Thanks.