Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
So I'm parsing a Http repsonse and I keep getting a nullpointer exception when I know a child element is there. This is my first XML rodeo so I was hoping for suggestions.

My Debug log before adding the getchildelement is this:

 

15:22:10:905 USER_DEBUG [27]|DEBUG|nodeXMLNode[ELEMENT,createCustomerProfileResponse,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,[common.apex.api.dom.XmlNode$NamespaceDef@21526738, common.apex.api.dom.XmlNode$NamespaceDef@582ef6d5, common.apex.api.dom.XmlNode$NamespaceDef@61835476],[XMLNode[ELEMENT,messages,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[ELEMENT,resultCode,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[TEXT,null,null,null,null,null,Error,]],null,], XMLNode[ELEMENT,message,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[ELEMENT,code,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[TEXT,null,null,null,null,null,E00039,]],null,], XMLNode[ELEMENT,text,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[TEXT,null,null,null,null,null,A duplicate record with ID 40545008 already exists.,]],null,]],null,]],null,], XMLNode[ELEMENT,customerPaymentProfileIdList,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,null,null,], XMLNode[ELEMENT,customerShippingAddressIdList,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,null,null,], XMLNode[ELEMENT,validationDirectResponseList,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,null,null,]],null,]

But when I add the getChildElement('resultCode',null).getText(); I'm getting a Null Pointer Exception even though the value is in the XML document. Any advice on what I'm missing?

 

public void process()

{

isSuccess = false;

string xml = getxmlRes();

Http h = new http();

HttpRequest req = new HttpRequest();

req.setEndpoint('https://apitest.authorize.net/xml/v1/request.api');

req.setMethod('POST');

req.setbody(xml);

HttpResponse res = h.send(req);

isSuccess = true;

Dom.Document doc = res.getBodyDocument();

status = res.getStatusCode();

Dom.XMLNode node = doc.getRootElement();

profilename = node.getChildElement('resultCode', null).gettext();

System.debug('ukpostcode: ' + node);

body = res.getBody();

}

 
3 answers
  1. Apr 28, 2016, 1:43 AM
    OK, I ran this through the FuseIT SFDC Explorer Wsdl2Apex (http://www.fuseit.com/explorer) implementation for the CreateCustomProfile web method. It appeared to generate the required Apex fine.

    I used the option to generate both the Wsdl2Apex equavlient code and the raw SOAP Http requests and parsing code.

    Now the inner ServiceSoap class has both a CreateCustomerProfile and CreateCustomerProfile_Http method. The latter converts the XML response into a apiAuthorizeNetSoapV1.CreateCustomerProfileResponse_element.

    I've posed the full code in https://gist.github.com/FishOfPrey/2e44344af0765a1eb9839df460bd0bb2
Loading
0/9000