1

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;

1 Answer 1

3

Your XML has a default namespace defined via the xmlns attribute so you will have to supply the namespace when referencing the elements:

String ns ='http://schemas.microsoft.com/search/local/ws/rest/v1';

if(objXMLNode.getChildElement('StatusDescription', ns).getText() == 'OK')  

You must log in to answer this question.

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