I have the below XML response which I am trying to parse through XML parser. But I am facing error i.e null pointer exception in debug log for the line:
if(objXMLNode.getChildElement('StatusDescription', null).getText() == 'OK')
of:
common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object
Code is:
xmlResponseString = '<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">' +
'<Copyright>Copyright © 2016 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>'+
'<BrandLogoUri>by.png</BrandLogoUri>'+
'<StatusCode>200</StatusCode>'+
'<StatusDescription>OK</StatusDescription>'+
'<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>'+
'<TraceId>8b</TraceId>'+
'<ResourceSets>'+
'<ResourceSet>'+
'<EstimatedTotal>1</EstimatedTotal>'+
'<Resources>'+
'<Route>'+
'<Id>n2</Id>'+
'<DistanceUnit>Mile</DistanceUnit>'+
'<DurationUnit>Second</DurationUnit>'+
'<TravelDistance>176.217763</TravelDistance>'+
'<TravelDuration>11447</TravelDuration>'+
'<TravelDurationTraffic>12344</TravelDurationTraffic>'+
'</Route>'+
'</Resources>'+
'</ResourceSet>'+
'</ResourceSets>'+
'</Response>'
;
}
system.debug('==================== xmlResponseString =============='+xmlResponseString);
// Dom Parsing to retrieve values
Dom.Document objDOM = new Dom.Document();
objDOM.load(xmlResponseString);
system.debug('******Before********');
Dom.XMLNode objXMLNode = objDOM.getRootElement();
system.debug('***************'+objXMLNode );
if(objXMLNode.getChildElement('StatusDescription', null).getText() == 'OK')
{
Integer rowIndex = 0;