+ Start a Discussion
Nkosilathi NcubeNkosilathi Ncube 

NULLPOINTER exception help

Hello Community, I have written this trigger to update County field for a particualr postcode, I am getting a NULLPOINTER Exception, please help
trigger TriggerUpdateContact on Contact(before insert, before update) {

    if(RecursiveTriggerHandler.isFirstTime){
        RecursiveTriggerHandler.isFirstTime = false;
}

    // querying all the name and Zipcode from County object
    List<PostCodes__c> countyList = [select Id, PostCode__c, County__c from PostCodes__c];   
   
    Map<String,String > PostCodeMap = new Map<String,String>();       
    
    //populating the Map
    for(PostCodes__c p : countyList ){
           PostCodeMap.put(p.PostCode__c,p.County__c);

       }  
    for (Contact c :Trigger.new){    
    string postcode2;
      
        if (c.MailingPostalCode.length() >= 8) {
                postcode2 = c.MailingPostalCode.substring(0,4);
        } else if (c.MailingPostalCode.length() >= 7)  {
                postcode2 = c.MailingPostalCode.substring(0,3);
        } else {
                postcode2 = c.MailingPostalCode.substring(0,2);
        }  
  
       
        if(c.MailingPostalCode!= null && PostCodeMap.containsKey(postcode2)) {
         c.County__c=PostCodeMap.get(postcode2);
                    
         }else
            c.County__c  = 'Unknown';
            
    
    }}
Best Answer chosen by Nkosilathi Ncube
sharathchandra thukkanisharathchandra thukkani
In your case if there is no postal code for the contact which you are inserting will cause null pointer exception. At line number 19 check if C.m ailingpostalcode != null then only execute next lines

All Answers

sharathchandra thukkanisharathchandra thukkani
In your case if there is no postal code for the contact which you are inserting will cause null pointer exception. At line number 19 check if C.m ailingpostalcode != null then only execute next lines
This was selected as the best answer
Anupama SamantroyAnupama Samantroy
Hi 

Please use the below code
trigger TriggerUpdateContact on Contact(before insert, before update) {

    if(RecursiveTriggerHandler.isFirstTime){
        RecursiveTriggerHandler.isFirstTime = false;
}

    // querying all the name and Zipcode from County object
    List<PostCodes__c> countyList = [select Id, PostCode__c, County__c from PostCodes__c];   
   
    Map<String,String > PostCodeMap = new Map<String,String>();       
    
    //populating the Map
    for(PostCodes__c p : countyList ){
		PostCodeMap.put(p.PostCode__c,p.County__c);

    }  
    for (Contact c :Trigger.new){    
		string postcode2;
		if(c.MailingPostalCode!= null && c.MailingPostalCode!=''){
			if (c.MailingPostalCode.length() >= 8) {
				postcode2 = c.MailingPostalCode.substring(0,4);
			} else if (c.MailingPostalCode.length() >= 7)  {
					postcode2 = c.MailingPostalCode.substring(0,3);
			} else {
					postcode2 = c.MailingPostalCode.substring(0,2);
			}  
		}    
       
        if(PostCodeMap.containsKey(postcode2)) {
			c.County__c=PostCodeMap.get(postcode2);                    
        }else{
            c.County__c  = 'Unknown';
		}            
    
    }
}



Thanks
Anupama
Nkosilathi NcubeNkosilathi Ncube
Thanks Anupama and Sharath, much appreciated