0

Having an issue parsing my json using Apex when the value is null. My Json2Apex is being returned like so.

[object1=cls_object:[amount=11000, amount_refunded=0, application=null, application_fee=null, balance_transaction=txn_1BB1D1Hh4a44STuoX4QnZGfh, captured=true, created=1507555335, currency1=gbp, customer=null, description=Test, destination=null, dispute=null, failure_code=null, failure_message=null, fraud_details=cls_fraud_details:[], id=ch_1BB1D1Hh4a44STuonu0URIAG, invoice=null, livemode=false, metadata=cls_metadata:[], object1=charge, on_behalf_of=null, order=null, outcome=cls_outcome:[network_status=approved_by_network, reason=null, risk_level=normal, seller_message=Payment complete., type=authorized], paid=true, [email protected], receipt_number=null, refunded=false, refunds=cls_refunds:[data=(), has_more=false, object1=list, total_count=0, url=/v1/charges/ch_1BB1D1Hh4a44STuonu0URIAG/refunds], review=null, shipping=null, source=cls_source:[address_city=Watton, address_country=GB, address_line1=3 east road, address_line1_check=pass, address_line2=null, address_state=null, address_zip=ip256ay, address_zip_check=pass, brand=Visa, country=US, customer=null, cvc_check=pass, dynamic_last4=null, exp_month=4, exp_year=2021, fingerprint=9riS9I9Iz8qRkF3K, funding=credit, id=card_1BB1CqHh4a44STuo5RG2v7B5, last4=4242, metadata=cls_metadata:[], name=Ben Allington, object1=card, tokenization_method=null], source_transfer=null, statement_descriptor=Test Statement, status=succeeded, transfer_group=null], outcome=null, source=null]

And I am parsing it using the controller class below.

public with sharing class StripePaymentController {
//Used to insert Person account from JSON response
public static Account accountInsert(JSON2Apex js) {
    RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Individual Giving - Person Account' and SObjectType = 'Account'];
    Account acc = new Account();
    acc.RecordType = personAccountRecordType;
    System.debug(js);
    if(js.source.name!=null){
        acc.name=js.source.name;
        }
    if(js.object1.receipt_email!=null){
        acc.personEmail=js.object1.receipt_email;
        }
    if(js.source.address_city!=null){
        acc.shippingCity=js.source.address_city;
        }
    if(js.source.address_country!=null){
        acc.shippingCountry=js.source.address_country;
        }
    if(js.source.address_zip!=null){
        acc.shippingPostalCode=js.source.address_zip;
        }
    if(js.source.address_line1!=null){
        acc.shippingStreet=js.source.address_line1;
        }
    if(js.source.address_line2!=null){
        acc.shippingStreet=acc.shippingStreet + ',' + js.source.address_line2;
    }

    insert acc;
    return acc; 
}
}

But I am getting this error on

line 8 : System.NullPointerException: Attempt to de-reference a null object

What am I doing wrong and what is the correct way to parse this checking if the values are null?

1 Answer 1

1

You're missing null check when you do a dot operator on source and object1 attributes.

You should add a null check before accessing the properties from object. Always keep an eye on line number in debug logs or in error, it helps in debugging when you are not sure.

if(js.source != null) {
    if(js.source.name!=null){
        acc.name=js.source.name;
    }
    if(js.source.address_city!=null){
        acc.shippingCity=js.source.address_city;
        }
    if(js.source.address_country!=null){
        acc.shippingCountry=js.source.address_country;
        }
    if(js.source.address_zip!=null){
        acc.shippingPostalCode=js.source.address_zip;
        }
    if(js.source.address_line1!=null){
        acc.shippingStreet=js.source.address_line1;
        }
    if(js.source.address_line2!=null){
        acc.shippingStreet=acc.shippingStreet + ',' + js.source.address_line2;
    }
}

if(js.object1 != null) {
    if(js.object1.receipt_email!=null){
        acc.personEmail=js.object1.receipt_email;
    }
}

Before inserting you might want to also add a check if all required fields are populated.

Having a try catch is always good.

0

You must log in to answer this question.

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