Skip to main content
edited tags
Link
Avesh
  • 137
  • 2
  • 5
  • 12
Source Link
Avesh
  • 137
  • 2
  • 5
  • 12

System.NullPointerException: Attempt to de-reference a null object Error while parsing XML response

I am trying to parse the XML response and want to create the records only when url is present but when i am trying to create the record then i am getting the below error "Line: 83, Column: 1 System.NullPointerException: Attempt to de-reference a null object"

XML Response:-

 <retours>
  <flux>
    <id>WStt3vWBFAW8K6R4luSRkdW</id>
    <retour>
      <publication>20180917114832</publication>
      <production>20180917114832</production>
      <nature>10</nature>
      <statut>OK</statut>
      <id>HJHyKLwgjl</id>
      <url>https://telechargement1.com</url>
    </retour>
    <retour>
      <publication>20180917114838</publication>
      <production>20180917114838</production>
      <nature>11</nature>
      <statut>OK</statut>
      <id>HrpiyLwmcA</id>
      <url>https://telechargement2.com</url>
    </retour>
    <retour>
      <publication>20180917122432</publication>
      <production>20180917122429</production>
      <nature>20</nature>
      <statut>ANO</statut>
      <id>LGvyKMYgpB</id>
      <url>https://telechargement3.com</url>
    </retour>
    <retour>
      <publication>20180917123004</publication>
      <production>20180917123004</production>
      <nature>21</nature>
      <statut>OK</statut>
    </retour>
    <retour>
      <publication>20180917115235</publication>
      <production>20180917114839</production>
      <nature>60</nature>
      <statut>OK</statut>
    </retour>
    <retour>
      <publication>20180917115240</publication>
      <production>20180917114839</production>
      <nature>61</nature>
      <statut>OK</statut>
      <id>FbhiyL0pPh</id>
      <url>https://telechargement4.com</url>
    </retour>
    <retour>
      <publication>20180917120305</publication>
      <production>20180917115540</production>
      <nature>70</nature>
      <statut>OK</statut>
    </retour>
    <retour>
      <publication>20180927135502</publication>
      <production>20180924140025</production>
      <nature>71</nature>
      <statut>OK</statut>
      <id>JKiyUN3CCI</id>
      <url>https://telechargement5.net-entreprises.com</url>
    </retour>
    <retour>
      <publication>20180922070825</publication>
      <production>20180922061355</production>
      <nature>94</nature>
      <statut>OK</statut>
      <id>KN3TeHIZ5X</id>
      <url>https://pascrm6.net-entreprises.com</url>
    </retour>
  </flux>
</retours>

Below is my Apex code with line causing the error:-

 List<DSN_retours__c> DsnRetourstoCreate=new List<DSN_retours__c>();
    for(Dom.XmlNode fluxNode: doc.getRootElement().getChildElements()) {
  for(Dom.XmlNode fluxChildNode: fluxNode.getChildElements()) {
    if(fluxChildNode.getName() == 'retour') {
 
           
            DSN_retours__c dsnretous=new DSN_retours__c(Name='Abc',Entete_DSN__c=dsnheadermap[0].id,
                    URL_telechargement_retour__c=fluxChildNode.getChildElement('url',null).getText(), //// **This line is causing the error**
                    Nature_retour__c=fluxChildNode.getChildElement('nature',null).getText(),
                    Statut_retour__c=fluxChildNode.getChildElement('statut',null).getText(),
                     Identifiant_retour__c= fluxChildNode.getChildElement('id',null).getText()                              
                                                   );
                
        DsnRetourstoCreate.add(dsnretous);
            
            
        
    }
  }
}
    
    System.debug('DSNRetourstoCreate size--->'+DsnRetourstoCreate.size());
    //insert DsnRetourstoCreate;
    //system.debug('urls.size--->'+urls.size());
    
    
}
else{
    System.debug('The status code returned was not expected: ' +
                 response1.getStatusCode() + ' ' + response1.getStatus());
    System.debug(response1.getBody());
}

I want to create the record in which Url is present if url tag is not present then it should skip the record creation