0

I'm a beginner developer and I'm building a custom new contract button with a visualforce page with dropdown field values that unhide other fields depending on what is selected.

Clicking the button takes me to the visualforce page with no issues, but when I try to select a dropdown value (in this example "Yes" for Contract Questionnaire) it brings me to the error below...

Attempt to de-reference a null object Error is in expression '{!setContractorQuestionnaire}' in page societycontractrequestform: Class.SocietyContractRequest.setContractorQuestionnaire: line 24, column 1

Visualforce Page

<apex:page standardcontroller="Contract" extensions="SocietyContractRequest" recordSetVar="sobjects">
    <Script>
        function setFocusOnLoad() {}
    </Script>    
      <apex:form >        
        <apex:pageMessages />                         
        <apex:pageBlock id="contractBlock" title="Contract Request Form" >            
            <apex:pageBlockSection columns="1" id="ContractInfo" showHeader="true" title="Contract Information" collapsible="false">
                <apex:inputField value="{!contract.Date_of_Request__c}" />
                <apex:inputField value="{!contract.Requester_Name__c}" />
                <apex:inputField value="{!contract.Project_Name__c}" />
                <apex:inputField value="{!contract.Project_Number__c}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1" id="ContractorInfo" showHeader="true" title="Contractor Information" collapsible="false">    
                <apex:inputField value="{!contract.Contractor_Legal_First_Name__c}" />
                <apex:inputField value="{!contract.Contractor_Legal_Last_Name__c}" />
                <apex:inputField value="{!contract.Operating_Name__c}" />
                <apex:inputField value="{!contract.Contractor_Street__c}" />
                <apex:inputField value="{!contract.Contractor_City__c}" />
                <apex:inputField value="{!contract.Contractor_State_Province__c}" />
                <apex:inputField value="{!contract.Contractor_Zip_Postal_Code__c}" />
                <apex:inputField value="{!contract.Contractor_Email_Address__c}" />
                <apex:inputField value="{!contract.Contractor_Phone_Number__c}" />
                <apex:inputField value="{!contract.Signing_Authority__c}" />
                <apex:inputField value="{!contract.Sub_Contracting__c}" />              
            </apex:pageBlockSection>
            
            <apex:pageBlockSection columns="1" id="ContractorQuestionnaire" showHeader="true" title="Contractor Questionnaire" collapsible="false">
            <apex:actionRegion >
                <apex:inputField value="{!contract.Questionnaire_Complete__c}" >
                    <apex:actionSupport event="onchange" action="{!setContractorQuestionnaire}" reRender="ContractorQuestionnaire"  />
                </apex:inputField>  
            </apex:actionRegion>    
                <apex:inputField value="{!contract.Questionnaire_Completion_Date__c}" rendered="{!ContractorQuestionnaire}" />
            </apex:pageBlockSection>    
            
            <apex:pageBlockSection columns="1" id="ContractorIncorpInfo" showHeader="true" title="Contractor Incorporation Information" collapsible="false">        
                <apex:actionRegion >
                <apex:inputField value="{!contract.Are_You_Incorporated__c}" >
                    <apex:actionSupport event="onchange" action="{!setContractorIncorpInfo}" reRender="ContractorIncorpInfo"  />
                </apex:inputField>  
                </apex:actionRegion>    
                <apex:inputField value="{!contract.Incorporation_Number__c}" rendered="{!ContractorIncorpInfo}"/>
                <apex:inputField value="{!contract.Incorporation_Province_Territory__c}" rendered="{!ContractorIncorpInfo}"/>
            </apex:pageBlockSection>    
            
            <apex:pageBlockSection columns="1" id="WorkersCompInfo" showHeader="true" title="Workers Compensation Information" collapsible="false"> 
                <apex:actionRegion >
                <apex:inputField value="{!contract.Workers_Compensation_Status__c}" >
                    <apex:actionSupport event="onchange" action="{!setWorkersCompInfo}" reRender="WorkersCompInfo"  />
                </apex:inputField>
                </apex:actionRegion>
                <apex:inputField value="{!contract.Workers_Compensation_A_C_Number__c}" rendered="{!WorkersCompInfo}"/>
                <apex:inputField value="{!contract.Workers_Compensation_Province_Territory__c}" rendered="{!WorkersCompInfo}"/>
                <apex:inputField value="{!contract.Proof_of_Exemption_Uploaded__c}" rendered="{!WorkersCompInfoEX}"/>
                <apex:inputField value="{!contract.Clearance_Certificate_Uploaded__c}" rendered="{!WorkersCompInfo}"/>
            </apex:pageBlockSection>    
            
            <apex:pageBlockSection columns="1" id="GSTHSTInfo" showHeader="true" title="GST/HST Information" collapsible="false">       
                <apex:actionRegion >
                <apex:inputField value="{!contract.Do_you_have_a_GST_HST_Number__c}" >
                    <apex:actionSupport event="onchange" action="{!setGSTHSTInfo}" reRender="GSTHSTInfo"  />
                </apex:inputField>  
                </apex:actionRegion>
                <apex:inputField value="{!contract.GST_HST_Number__c}" rendered="{!GSTHSTInfo}"/>
            </apex:pageBlockSection>    
            
            <apex:pageBlockSection columns="1" id="MonitorDeliverables" showHeader="true" title="Monitoring of Deliverables" collapsible="false">   
                <apex:inputField value="{!contract.Deliverables_Supervisor_Name__c}" />
                <apex:inputField value="{!contract.Deliverables_Supervisor_Title__c}" />
            </apex:pageBlockSection>    
            
            <apex:pageBlockSection columns="1" id="SourceOfFunds" showHeader="true" title="Source of Funds" collapsible="false">    
                <apex:inputField value="{!contract.Source_Award_Code__c}" />
                <apex:inputField value="{!contract.Source_Program_Area__c}" />
            </apex:pageBlockSection>    
            
            <apex:pageBlockSection columns="1" id="ContractValueInfo" showHeader="true" title="Contract Value Information" collapsible="false"> 
                <apex:inputField value="{!contract.Fee_for_Service_Amount__c}" />
                <apex:inputField value="{!contract.Fee_for_Service_Additional_Notes__c}" />
                <apex:inputField value="{!contract.Expenses_Amount__c}" />
                <apex:outputField value="{!contract.Total_Contract_Value__c}" />
            </apex:pageBlockSection>    
            
            <apex:pageBlockSection columns="1" id="ContextOfWork" showHeader="true" title="Context of Work" collapsible="false">        
                <apex:inputField value="{!contract.Context_of_Work__c}" />
            </apex:pageBlockSection>    
            
            <apex:pageBlockSection columns="1" id="DescriptionOfWork" showHeader="true" title="Description of Work" collapsible="false">    
                <apex:inputField value="{!contract.Description_of_Work__c}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
             
        <apex:pageMessages />
        <apex:commandButton action="{!saveForm}" value="Save Contract" />        
        <apex:commandButton action="{!cancelPage}" value="Cancel" immediate="true" />                
    </apex:form> 
</apex:page>

Controller Extension...

public with sharing class SocietyContractRequest{
        
    public Contract Contract {get;set;}    
    
    public Boolean ContractorQuestionnaire{get;set;}
    public Boolean ContractorIncorpInfo{get;set;}
    public Boolean WorkersCompInfo{get;set;}
    public Boolean WorkersCompInfoEX{get;set;}
    public Boolean GSTHSTInfo{get;set;}
    
    private ApexPages.StandardSetController ssc;
    private ApexPages.StandardController controller;
    
    public SocietyContractRequest(ApexPages.StandardController controller){
        this.controller = controller;
    }
    
    public SocietyContractRequest(ApexPages.StandardSetController controller){
        this.ssc = controller;
    }
    
    //If Questionnaire Complete is Yes then render the Contractor Questionnaire detail fields
    public void setContractorQuestionnaire(){
        if (contract.Questionnaire_Complete__c == 'Yes' ){
            ContractorQuestionnaire = true;
        }else{
            ContractorQuestionnaire = false;            
        }
    }
    
    //If Contractor Incorporated is Yes then render the Contractor Incorporation detail fields
    public void setContractorIncorpInfo(){
        if (contract.Are_You_Incorporated__c == 'Yes' ){
            ContractorIncorpInfo = true;
        }else{
            ContractorIncorpInfo = false;            
        }
    }   
    
        //If Workers Compensation Status is Yes then render the Contractor Questionnaire detail fields
    public void setWorkersCompInfo(){
        if (contract.Workers_Compensation_Status__c == 'Owns WC Account' ){
            WorkersCompInfo = true;
            WorkersCompInfoEX = false;
        }else if (contract.Workers_Compensation_Status__c == 'Exempt from WC' ){
            WorkersCompInfo = false;
            WorkersCompInfoEX = true;
        }else {
            WorkersCompInfo = false;
            WorkersCompInfoEX = false;
        }
    }
    
        //If Questionnaire Complete is Yes then render the Contractor Questionnaire detail fields
    public void setGSTHSTInfo(){
        if (contract.Do_you_have_a_GST_HST_Number__c == 'Yes' ){
            GSTHSTInfo = true;
        }else{
            GSTHSTInfo = false;            
        }
    }
    
    
    //Save page
    public PageReference saveForm(){
        system.debug('Saving Contract...');
        upsert contract;
        PageReference pageRef = new PageReference('/' + contract.Id);
        pageRef.setRedirect(true);
        return pageRef;
       }
       
    //Cancel and return to the CRA Activity Report page
    public PageReference cancelPage(){
        PageReference contractPage = new ApexPages.StandardController(contract).view();
        contractPage.setRedirect(true);
        return contractPage;
    }
}
0

It is likely that you contract is null. You will need to set it in the constructor.

this.contract = (Contract)stdController.getRecord();

| improve this answer | |

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.