+ Start a Discussion
Huy NguyenHuy Nguyen 

System.NullPointerException: Attempt to de-reference a null object

I got the error in the subtitle . Can any one help me to fix that . I below code i can not create the disclosure obligation when the name has & symbol. It is created ok with normal name

Here is my below code :

/**
    @description    Class for the Redirect With Variables page.     
    Function: Handles the controller logic for the RedirectWithVariables page.       
 */

public with sharing class RedirectWithVariablesController {
    /*
     *  Method name:    redirectUser
     *  @description    Redirects the user to the given object + parameters
     *  @return         PageReference The page the user needs to be redirect to.
     */    
    public pagereference prePopulateFields(){
        //Get object name
        String strObject = System.currentPageReference().getParameters().get('object');  
        string params = '';
        Integer a = 0;
        map<String,String> fields = getFieldsIdsMap(strObject);
        
        string regName = System.currentPageReference().getParameters().get('Registration__c');
        string regId = System.currentPageReference().getParameters().get('ID_Registration__v');
        string retURL = System.currentPageReference().getParameters().get('retURL');
        string stakeholerName = System.currentPageReference().getParameters().get('Stakeholder__c');
        string stakeholerId = System.currentPageReference().getParameters().get('ID_Stakeholder__v');
        for(string k: fields.keySet()){
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, k+''));
            //System.debug('label: ' + a + ': ' + k);
            //a++;
            if(k == 'Registration'){
                params += fields.get(k) + '=' + regName + '&' + fields.get(k) + '_lkid=' + regId + '&';
            }
            if(k == 'Stakeholder'){
                params += fields.get(k) + '=' + stakeholerName + '&' + fields.get(k) + '_lkid=' + stakeholerId+ '&';                
            }
        }
        //for(string p:fields.values()){            
        //    params += p+'=Test Value '+a+'&';
        //    a++;
        //}
        PageReference p = new PageReference('/' +  
        Schema.getGlobalDescribe().get(strObject).getDescribe().getKeyPrefix() + '/e?'+
            params+'nooverride=1&retURL=' + retURL);        
        p.setRedirect(true);
        return p;
    }

    public map<String, String> getFieldsIdsMap(String sObjectName) {
        map<String, String> label_IdMap = new map<String, String>();
        if(null!= sObjectName && sObjectName.trim() != '') {
            //if(!isObjectValid(sObjectName)){
            //    return null;
            //}
            PageReference p = new PageReference('/' +  
            Schema.getGlobalDescribe().get(sObjectName).getDescribe().getKeyPrefix()
            + '/e?nooverride=1'); // this one return null in the url
            String html = '';
            if(!Test.isRunningTest()) {
                html = p.getContent().toString();
            } else {
                //html = '';
                html = '<label for="CF00NK0000000Yt1b"><span class="requiredMark">*</span>Name</label>';
            }
            Matcher m = Pattern.compile('<label for="(.*?)">(<span class="requiredMark">\\*</span>)?(.*?)</label>').
            matcher(html);
            while(m.find()) {
                String label = m.group(3);
                String id = m.group(1);
                label_IdMap.put(label, id);
            }
        }
        return label_IdMap;
    }    
}

controller:
public class DisclosureNewOverrideController{
    public String retURL {get;set;}
    public DisclosureNewOverrideController(ApexPages.standardController sc){
        retURL = ApexPages.CurrentPage().GetParameters().Get('retURL');
        system.debug('nghiatran retURL ' + retURL + ' ' + retURL.substring(1));
        //ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'This: ' + retURL));
        //string objectName = getObjectName(retURL);
        string objectName = CommonUtils.getObjectName(retURL.substring(1));
        if(objectName == 'Disclosure_Claim_for_Payment__c'){
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'This button has been disabled to help ensure the correct Disclosure/Claim for Payment is created.'));
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'To create a New Disclosure, please navigate to the Stakeholder record -> Disclosure Obligation record.'));
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'To create a New Claim for Payment, please navigate to the Stakeholder record -> Eligible Claim for Payment record.'));
        }else if(objectName == 'Disclosure_Obligation__c'){
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'This button has been disabled to help ensure the correct Disclosure Obligation is created.'));
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'To create a new Disclosure Obligation, please navigate to the Stakeholder record.'));
        }else if(objectName == 'HP_Trim_Container__c'){
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'This button has been disabled to help ensure the correct Donor TRIM Containers are created.'));
            ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'To create the Donor TRIM Containers, please navigate to the \'All\' list view and click on \'Generate Donor TRIM Containers.\''));
        }
    }
   

this is 2 url when create new disclosure :

not ok https://c.cs6.visual.force.com/apex/DisclosureObligationNewOverride?save_new=1&sfdc.override=1
 the account name :Mr Madhvi Lata & Visapaa
ok :https://c.cs6.visual.force.com/apex/DisclosureObligationNewOverride?CF00NN0000000MBK7=Aegis+Consulting+Group+Pty+Ltd&CF00NN0000000MBK7_lkid=001N000000M1lpg&scontrolCaching=1&retURL=%2F001N000000M1lpg&sfdc.override=1
the account name :Aegis Consulting Group Pty Ltd



 
Best Answer chosen by Huy Nguyen
Swati GSwati G
Use 
String encoded = EncodingUtil.urlEncode(url, 'UTF-8'); whenever you passing any parameter in url.

 if(k == 'Registration'){
        params += fields.get(k) + '=' + regName + '&' + fields.get(k) + '_lkid=' + regId + '&';
 }
 if(k == 'Stakeholder'){
         params += fields.get(k) + '=' + stakeholerName + '&' + fields.get(k) + '_lkid=' + stakeholerId+ '&';                
  }

In the above code, replace regName with EncodingUtil.urlEncode(regName, 'UTF-8') and replace stakeholerName with EncodingUtil.urlEncode(stakeholerName, 'UTF-8') 
EncodingUtil: ttps://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_encodingUtil.htm#apex_System_EncodingUtil_urlEncode

All Answers

Swati GSwati G
Use 
String encoded = EncodingUtil.urlEncode(url, 'UTF-8'); whenever you passing any parameter in url.

 if(k == 'Registration'){
        params += fields.get(k) + '=' + regName + '&' + fields.get(k) + '_lkid=' + regId + '&';
 }
 if(k == 'Stakeholder'){
         params += fields.get(k) + '=' + stakeholerName + '&' + fields.get(k) + '_lkid=' + stakeholerId+ '&';                
  }

In the above code, replace regName with EncodingUtil.urlEncode(regName, 'UTF-8') and replace stakeholerName with EncodingUtil.urlEncode(stakeholerName, 'UTF-8') 
EncodingUtil: ttps://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_encodingUtil.htm#apex_System_EncodingUtil_urlEncode
This was selected as the best answer
Huy NguyenHuy Nguyen
Hi Swati. It is working . Thank a lot :)