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
VARUN GULATI 30VARUN GULATI 30 

Need Help || Create Custom & Custom Related Object XML from a Button Click || Attempt to de-reference a null object

Greetings Experts,

I wish to create an XML and view on on a Button Click for fields of 2 Related Custom Objects:
Inspection: Master
Inspection Detail: Child

However, I am getting below Error while clicking on Button (Generate Certificate). Appreciate your help and guidance with this:

Attempt to de-reference a null object
Error is in expression '{!InspectionHierarchyInXML}' in component <apex:commandButton> in page inspectiondetail: Class.InspectionControllerExtension.InspectionHierarchyInXML: line 25, column 1
An unexpected error has occurred. Your development organization has been notified.

Here is a Visualforce Page that I have created:

<apex:page standardController="Inspection__c" extensions="InspectionControllerExtension" >
    <apex:form >
        <apex:pageBlock >
            <apex:detail relatedList="false"/>
            <apex:pageBlockSection >
                <apex:commandButton value="Generate Certificate" action="{!InspectionHierarchyInXML}"
                 rendered="{!IF(Inspection__c.Status__c == 'Pass',true,false)}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock title="Inspection Items" mode="inlineEdit" >
            <apex:pageBlockButtons location="top">
                <apex:commandButton action="{!save}" value="Save" id="saveButton" />
                <apex:commandButton onclick="resetInlineEdit()" id="cancelButton" value="Cancel" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!Inspection_Checks}" var="item">
                <apex:column value="{!item.Product_Check_Name__c}" headerValue="Inspection Check Name"/>
                <apex:column headerValue="Inspection Check Status">
                    <apex:outputField value="{!item.Status__c}">
                        <apex:inlineEditSupport showOnEdit="saveButton,cancelButton"
                        hideOnEdit="editButton" event="ondblclick"
                        changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/>
                    </apex:outputField>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>

        /* Below is the Code for Auto Refresh on the Page */
        
        <apex:outputPanel id="refresh" rendered="true">
             <apex:outputPanel id="refresh1" rendered="{!refreshPage}">
              <script>
                   window.top.location='/{!Inspection__c.id}';
              </script>
             </apex:outputPanel>
        </apex:outputPanel>
    </apex:form>
</apex:page>


Also, below is the Custom Extension Class:

public class InspectionControllerExtension {

    public List<Inspection_Checks__c> Inspection_Checks {get;set;}

        public InspectionControllerExtension(ApexPages.StandardController controller) {
        Inspection_Checks = [select id,Product_Check_Name__c,Status__c from Inspection_Checks__c where Inspection__c =:controller.getId()];
    }
    
    public Boolean refreshPage {get; set;}
    
    public InspectionControllerExtension() {
        refreshPage=false;
    }
    
    public PageReference save() {
        update Inspection_Checks ;
        refreshPage=true;
        return null;
    }
    
    public ApexPages.StandardController myController;
    public PageReference InspectionHierarchyInXML() {
    
    //Id recordId = new Id();
    Id recordId = myController.getId();
    Dom.Document doc = new Dom.Document();    
    Dom.Xmlnode rootNode = doc.createRootElement('response', null, null);
    Inspection__c inspectionTarget = [ select  id, name, Opportunity__c, Product__c,
                           (select id, name from Inspection_Checks__r) from Inspection__c 
                                      where Id = :recordId ];
        
        Dom.Xmlnode accountNode = rootNode.addChildElement('Inspection__c', null, null);
        accountNode.setAttribute('id', inspectionTarget.Id);
        accountNode.setAttribute('name', inspectionTarget.Name);
        
        for (Inspection_Checks__c eachInspectionCheck : inspectionTarget.Inspection_Checks__r) {
            Dom.Xmlnode InspectionCheck = accountNode.addChildElement('Inspection_Checks__c', null, null);
            InspectionCheck.setAttribute('id', eachInspectionCheck.Id);
            InspectionCheck.setAttribute('name', eachInspectionCheck.Name);
        }

        System.debug(doc.toXmlString());
        return null;
}
    
}