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
Manish DeshmukhManish Deshmukh 

how to solve System.NullPointerException: Attempt to de-reference a null object Trigger.updateContact: line 34, column 1: []

  1. trigger updateContact on Opportunity (after insert,after update) {
  2.     
  3.     if(Trigger.isInsert || Trigger.isUpdate ){
  4.      
  5.         Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>(); 
  6.         List<Id> contactIdList = new List<Id>();
  7.         Map<Id, Contact> mapcontact = new Map<Id, Contact>();
  8.         Map<Id, List<Contact>> opptyContactMap = new Map<Id, List<Contact>>();
  9.         for(Opportunity opp : Trigger.new){
  10.             oppMap.put(opp.Id, opp);
  11.         }
  12.         //Contact[] contactList = [ SELECT Id, Nurture_Path__c FROM Contact WHERE Id IN ( SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId IN: oppMap.keySet()) ];
  13.         
  14.         OpportunityContactRole[] opptyConRole = [SELECT ContactId, OpportunityId FROM OpportunityContactRole
  15.                                              WHERE OpportunityId IN: oppMap.keySet()];
  16.         
  17.         Map<Id, List<ID>> opptyContIdMap = new Map<Id, List<ID>>();
  18.         
  19.         for(OpportunityContactRole ocr : opptyConRole){
  20.             
  21.             if(opptyContIdMap.containsKey(ocr.OpportunityId))
  22.                 contactIdList = opptyContIdMap.get(ocr.OpportunityId);
  23.             else
  24.                 contactIdList = new List<Id>();
  25.             
  26.             contactIdList.add(ocr.ContactId);
  27.             opptyContIdMap.put(ocr.OpportunityId, contactIdList);
  28.         }
  29.         
  30.         for(Opportunity oppty : oppMap.values() ) {
  31.             system.debug('opptyContIdMap==>'+opptyContIdMap);
  32.             if(opptyContIdMap != NULL && !opptyContIdMap.isEmpty() ){
  33.                 if(oppty.stageName.contains('Prospecting')){
  34.                     for(Id idr : opptyContIdMap.get(oppty.Id)){
  35.                         Contact con = new contact(id = idr);
  36.                         if(!mapcontact.containsKey(con.id)){
  37.                             con.Nurture_Path__c = 'PreQual Op Nurture';
  38.                             mapcontact.put(con.id, con);
  39.                         }
  40.                     }
  41.                 }
  42.             else{
  43.                 for(Id idr : opptyContIdMap.get(oppty.Id)){
  44.                     Contact con = new contact(id = idr);
  45.                     if(!mapcontact.containsKey(con.id)){
  46.                         con.Nurture_Path__c = '';
  47.                         mapcontact.put(con.id, con);
  48.                     }
  49.                 }
  50.                     
  51.             }
  52.             }
  53.         }
  54.         List<Contact> conToUpdate = mapcontact.values();
  55.         if(conToUpdate!= NULL && !conToUpdate.isEmpty())
  56.         update conToUpdate;
  57.     }  
  58. }
Manish DeshmukhManish Deshmukh
Yes , getting the error within an Apex test class
My Requirement is -: 

1.IF contact is associated to an opportunity and opportunity stage = "PreQual" THEN update "Nurture Path" field to "PreQual Op Nurture"

2.IF contact associated to an opportunity and opportunity state = "Qualified", "Discovery", "Solution Overview", "Scoping", "Proposal", "Pending Approval","Won", "Closed Lost", or "Cancelled" THEN clear out "Nurture Path" Field to <None>

above trigger is working fine, but when i update Bulkify Opportunity at that time it will throw above error msg in test class