2

I have below code that fails to execute in developer console, I receive the following error: Attempt to de-reference a null object. Can someone please help here?

Apex class:

public class DomDocument1 {

    public void parseResponseDom(){

       string xml = '<Order>pl.map_00NF0000008ZKQu_012F00000011jfx={\'Lupron Ped Call Plan update\':\'fwAA\'}; pl.vals_00NF0000008ZKQu_012F00000011jfx=[\'N/A\',\'N/A\',\'Include - High\',\'Include - High\',\'Include - Medium\',\'Include - Medium\',\'Include - Low\',\'Include - Low\',\'Remove - Deceased/Retired\',\'Remove - Deceased/Retired\',\'Remove - Moved out\']</Order>';
       Dom.Document doc = new Dom.Document();
       doc.load(xml);

        Dom.XMLNode ordDtls = doc.getRootElement();

        String OrderDetailId= ordDtls.getChildElement('pl.map_00NF0000008ZKQu_012F00000011jfx', null).getText();

        System.debug('OrderDtlID: ' + OrderDetailId);

        for(Dom.XMLNode child : ordDtls.getChildElements()) {
           System.debug(child.getText());
        }
    }
}
  • 1
    Which line are you getting the error on? – Alex Tennant Jun 26 '15 at 8:52
  • The error is happening on the line that calls ordDtls.getChildElement() but ordDtls is not null. – BarCotter Jun 26 '15 at 9:53
3

I see that your XML string contains one single node, Order, and its content is a JSON string, not having children XML nodes, so you can't invoke getChildElement() with a non-existent subnode.

Try to change xml variable content to:

<Order>
<pl.map_00NF0000008ZKQu_012F00000011jfx>
  {\'Lupron Ped Call Plan update\':\'fwAA\'};
</pl.map_00NF0000008ZKQu_012F00000011jfx>
<pl.vals_00NF0000008ZKQu_012F00000011jfx>
  [\'N/A\',\'N/A\',\'Include - High\',\'Include - High\',\'Include - Medium\',\'Include - Medium\',\'Include - Low\',\'Include - Low\',\'Remove - Deceased/Retired\',\'Remove - Deceased/Retired\',\'Remove - Moved out\']</pl.vals_00NF0000008ZKQu_012F00000011jfx>
</Order>

Remove extra spaces when paste into single quotes - or concatenate pieces as you want - and re-run your code.

  • So how can I resolve it? Can you please suggest? – Testing_SFDC Jun 26 '15 at 9:00
  • 1
    Set XML string as follows: string xml = '<Order><pl.map_00NF0000008ZKQu_012F00000011jfx>{\'Lupron Ped Call Plan update\':\'fwAA\'}; pl.vals_00NF0000008ZKQu_012F00000011jfx=[\'N/A\',\'N/A\',\'Include - High\',\'Include - High\',\'Include - Medium\',\'Include - Medium\',\'Include - Low\',\'Include - Low\',\'Remove - Deceased/Retired\',\'Remove - Deceased/Retired\',\'Remove - Moved out\']</pl.map_00NF0000008ZKQu_012F00000011jfx></Order> - check syntax with a browser (or JSFiddle?) before using it! – frevabogo Jun 26 '15 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.