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());
        }
    }
}
2
  • 1
    Which line are you getting the error on? Commented Jun 26, 2015 at 8:52
  • The error is happening on the line that calls ordDtls.getChildElement() but ordDtls is not null.
    – BarCotter
    Commented Jun 26, 2015 at 9:53

1 Answer 1

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.

2
  • So how can I resolve it? Can you please suggest? Commented Jun 26, 2015 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
    Commented Jun 26, 2015 at 9:04

You must log in to answer this question.

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