function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian Cherry FWIBrian Cherry FWI 

Parsing XML Dom Null Pointer

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();
}

 
Nayana KNayana K
You should traverse from top to child element.

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();
Dom.XMLNode CPR = node.getChildElement('createCustomerProfileResponse', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd');
Dom.XMLNode msgs = CPR.getChildElement('messages', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd');
Dom.XMLNode profilename = msgs.getChildElement('resultCode', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd');

system.debug('====profilename====+profilename);
System.debug('ukpostcode: ' + profilename);

}