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
Tom MujoTom Mujo 

Weird System.NullPointerException with XML Child Element - PLEASE HELP!

Guys,

 

I've been working away on pulling one child element from an HTTPresponse into SFDC and I am so nealr ythere. I am complete noob with this but I get the sense I am close.

 

I can pull the full XML into the record - now I just need to refine it down to a single filed. The issue is I am getting a Null error from the field - but I know its there!!!!!

 

Here's the XML.

 

 

 

<result>
  <postcode>WA16 9NG</postcode> 
- <geo>
  <lat>53.247372</lat> 
  <lng>-2.34704</lng> 
  <easting>376939</easting> 
  <northing>372317</northing> 
  <geohash>http://geohash.org/gcqqft6uqj84</geohash> 
  </geo>
- <administrative>
- <constituency>
  <title /> 
  <uri /> 
  <code /> 
  </constituency>
- <district>
  <title>Cheshire East Council</title> 
  <uri>http://statistics.data.gov.uk/id/local-authority/00EQ</uri> 
  <snac>00EQ</snac> 
  </district>
- <ward>
  <title /> 
  <uri>http://statistics.data.gov.uk/id/electoral-ward/00EQMG</uri> 
  <snac>00EQMG</snac> 
  </ward>
  </administrative>
  </result>

 I am trying to get the title of the district into my SFDC.

 

So clearly the data is there. However, here's my debug...

 

 

9:0:4.895|USER_DEBUG|[30,5]|DEBUG|ukpostcode: XMLNode[ELEMENT,result,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,postcode,null,null,null,[XMLNode[TEXT,null,null,null,null,null,WA16 9NG,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,geo,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,lat,null,null,null,[XMLNode[TEXT,null,null,null,null,null,53.247372,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,lng,null,null,null,[XMLNode[TEXT,null,null,null,null,null,-2.34704,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,easting,null,null,null,[XMLNode[TEXT,null,null,null,null,null,376939,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,northing,null,null,null,[XMLNode[TEXT,null,null,null,null,null,372317,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,geohash,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://geohash.org/gcqqft6uqj84,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,administrative,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,constituency,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,code,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,district,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,[XMLNode[TEXT,null,null,null,null,null,Cheshire East Council,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://statistics.data.gov.uk/id/local-authority/00EQ,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,snac,null,null,null,[XMLNode[TEXT,null,null,null,null,null,00EQ,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,ward,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://statistics.data.gov.uk/id/electoral-ward/00EQMG,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,snac,null,null,null,[XMLNode[TEXT,null,null,null,null,null,00EQMG,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,]],null,], XMLNode[TEXT,null,null,null,null,null,
,]],null,]

 

9:0:4.895|METHOD_EXIT|[30,5]|System.debug(String)
9:0:4.895|METHOD_ENTRY|[33,63]|dom.XmlNode.getName()
9:0:4.895|METHOD_ENTRY|[33,22]|dom.XmlNode.getChildElement(String, NULL)
9:0:4.895|METHOD_EXIT|[33,22]|dom.XmlNode.getChildElement(String, NULL)
9:0:4.895|EXCEPTION_THROWN|[33,22]|System.NullPointerException: Attempt to de-reference a null object
9:0:4.895|METHOD_EXIT|[33,63]|dom.XmlNode.getName()
9:0:4.895|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Look at all those nulls before the data is presented. Is that normal - is that what's causing my exception?

 

Here's my class - any help you can give me would be very much appreciated....

 

 

public class IncidentCouncilUpdater {

  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void updateIncident(String id, String name) {

    //construct an HTTP request
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://www.uk-postcodes.com/latlng/53.24354,-2.34567.xml');
    req.setMethod('GET');
    req.setTimeout(60000);
    
    //send the request
    Http http = new Http();
    HttpResponse res = http.send(req);
    
    // Log the XML content  
    Dom.Document doc = res.getBodyDocument();
    String district_title;
    
    //Retrieve the root element for this document.     
    Dom.XMLNode ukpostcode = doc.getRootElement();
    
    // print out specific elements by finding the specific node location 
    dom.XmlNode location = doc.getRootElement()
    .getChildElement('administrative',null)
    .getChildElement('district',null);  
    
    System.debug('ukpostcode: ' + ukpostcode);  
    
    // gets the content from the XML
    district_title = ukpostcode.getChildElement('title',null).getText();
   
      //update Incident
      Incident__c Inc = new Incident__c(Id=id);
      Inc.XMLLong__c = district_title;
    

 

Thanks in advance guys.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Tom MujoTom Mujo

Ignore me.

 

I have figured this out. I was drilling too far down into the nodes.

All Answers

Tom MujoTom Mujo

Ignore me.

 

I have figured this out. I was drilling too far down into the nodes.

This was selected as the best answer
TechnozChampTechnozChamp

Please elaborate what you figured out.what was the error

 

I am facing same problem.

 

Thanks

Amit

TechnozChampTechnozChamp

To be more specific-

here is my xml

 

   <Client xmlns="">

      <PolicyInfo>

         <MEC/>

         <InitPrem>100000.00</InitPrem>

         <InitDb>0.00</InitDb>

      </PolicyInfo>

      <Answers>

         <Keyword id="pol_ProductName" type="normal" matrix="N" active="1" xsi:type="NormalQuestionType">

            <Answer>sdfsdfsd</Answer>

         </Keyword>

         <Keyword id="Broker.Name" type="normal" matrix="N" active="1" xsi:type="NormalQuestionType">

            <Answer/>

         </Keyword>

         <Keyword id="prod_Code" type="normal" matrix="N" active="1" xsi:type="NormalQuestionType">

            <Answer>sdfsdfs</Answer>

         </Keyword>

         <Keyword id="Policy.FaceAmount" type="yearly" matrix="N" active="1" xsi:type="YearlyQuestionType">

            <Yearly>

               <Start>1</Start>

               <End>99</End>

               <Answer>999999</Answer>

 

 

i want to access the node keyword with id=prod_code. How can i traverse there.