+ Start a Discussion
happy devhappy dev 

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

I have trigger to update Account depending on the order call data input.  The first and the third checking work and can update Account successfully.  However, the second one checking the "'1yr RP Quit" always shows the error "System.NullPointerException: Attempt to de-reference a null object" when it does "acclist.get(oc.Member__c).X1yr_Reserve_Program__c = FALSE".  Anyone can know why?

 

 

  Set<Id> accIds = new Set<Id>();

  for(Order_Call__c oc: Trigger.new){  

      if(oc.Call_Purpose__c == 'Order' || oc.Agreed_to_join_1yrRP__c == True ||  

         oc.Call_Purpose__c == '1yr RP Quit'){  

         accIds.add(oc.Member__c);

      }//end if    

  }//end for     

  Map<Id,Account> acclist = new Map<Id,Account>([select id, Last_Order_Date__c, X1yr_Reserve_Program__c, X1yrRP_Joined_Date__c,

              X1yrRP_Quit_Date__c, No_Of_Orders__c from Account where id in :accIds]);       

  for(Order_Call__c oc:Trigger.new){           

      if(oc.Call_Purpose__c == 'Order'){

         acclist.get(oc.Member__c).Last_Order_Date__c = oc.Order_Date__c;    

         string a = string.valueOf(acclist.get(oc.Member__c).No_of_Orders__c);      

         if(a == NULL){      

            acclist.get(oc.Member__c).No_of_Orders__c = 0;         

         }//end if 

         acclist.get(oc.Member__c).No_of_Orders__c = acclist.get(oc.Member__c).No_of_Orders__c + 1;      

      //end if        

      if(oc.Call_About__c == '1yr RP Quit'){        

         acclist.get(oc.Member__c).X1yr_Reserve_Program__c = FALSE;

         acclist.get(oc.Member__c).X1yrRP_Quit_Date__c = date.today();       

      }//end if         

      if(oc.Agreed_to_join_1yrRP__c == True){          

         acclist.get(oc.Member__c).X1yr_Reserve_Program__c = TRUE;

         acclist.get(oc.Member__c).X1yrRP_Joined_Date__c = date.today();

      }//end if            

  }//end for          

 

  update(acclist.values());

 

}

vishal@forcevishal@force

  if(oc.Call_About__c == '1yr RP Quit'){        

         acclist.get(oc.Member__c).X1yr_Reserve_Program__c = FALSE;

         acclist.get(oc.Member__c).X1yrRP_Quit_Date__c = date.today();       

 

 

Is this where you get the null pointer exception? 

I would suggest you to add a condition check to ensure that the key is present in the ma[, and this should be done not only inside this if, but also in other if's where you are directly using map.get(your_key);

 

 

So, add the following check and see if it works.

 

  if(oc.Call_About__c == '1yr RP Quit'){        

        if(acclist.containsKey(oc.Member__c) && acclist.get(oc.Member__c) != NULL){

               acclist.get(oc.Member__c).X1yr_Reserve_Program__c = FALSE;

               acclist.get(oc.Member__c).X1yrRP_Quit_Date__c = date.today();       

          }

          ...........

          ...........

}

happy devhappy dev

Strange is why the third set of checking works.  If I check the field Agreed to join 1yrRP when data entry, both X1yr_Reserve_Program__c and X1yrRP_Joined_Date__c are updated correctly.  The only difference is Call_About__c is a picklist and Agreed_to_join_1yrRP__c is a check box but Call_About__c is a picklist field depending on Call_About__c.  Would that have any impact?

happy devhappy dev

I've tried different scenerio and found that there is error if the program only to complete that set of updates.  It works fine if that set of updates is done together with the first or the third one. 

happy devhappy dev

I have changed the code as below.  There is no error but also does not do the update

 

                  acclist.get(acc).X1yr_Reserve_Program__c = FALSE;
                  acclist.get(acc).X1yrRP_Quit_Date__c = date.today();

 

Any help?

 

trigger is

 

Set<Id> accIds = new Set<Id>();
for(Order_Call__c oc: Trigger.new){
      if(oc.Call_Purpose__c == 'Order' || oc.Agreed_to_join_1yrRP__c == True || oc.Call_Purpose__c == '1yr RP Quit'){
         accIds.add(oc.Member__c);
      }//end if
}//end for

Map<Id,Account> acclist = new Map<Id,Account>([select id, Last_Order_Date__c, X1yr_Reserve_Program__c, X1yrRP_Joined_Date__c,
X1yrRP_Quit_Date__c, No_Of_Orders__c from Account where id in :accIds]);

for(Order_Call__c oc:Trigger.new){
     if(oc.Call_Purpose__c == 'Order' || oc.Call_About__c == '1yr RP Quit' || oc.Agreed_to_join_1yrRP__c){ 
       for(Id acc: acclist.keySet()){ 
            if(oc.Member__c == acclist.get(acc).Id){
               if(oc.Call_Purpose__c == '1yr RP Arrangement' && oc.Call_About__c == '1yr RP Quit'){
                  acclist.get(acc).X1yr_Reserve_Program__c = FALSE;
                  acclist.get(acc).X1yrRP_Quit_Date__c = date.today(); 

               }//end if
               if(oc.Call_Purpose__c == 'Order'){
                  acclist.get(acc).Last_Order_Date__c = oc.Order_Date__c; 
                  string a = string.valueOf(acclist.get(acc).No_of_Orders__c); 
                  if(a == NULL){ 
                     acclist.get(acc).No_of_Orders__c = 0; 
                 }//end if 
                 acclist.get(acc).No_of_Orders__c = acclist.get(acc).No_of_Orders__c + 1; 
              }//end if

          if(oc.Agreed_to_join_1yrRP__c == True){ 
             acclist.get(acc).X1yr_Reserve_Program__c = TRUE; 
             acclist.get(acc).X1yrRP_Joined_Date__c = date.today();
          }//end if

happy devhappy dev

prolem is solved