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
SivaGSivaG 

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

Hi,

I am getting 'System.NullPointerException: Attempt to de-reference a null object' exception @ line 15(if(accupdt)). Please help what's wrong with the below code.

My requirement is simple for every General Req obj & Act if Merchant/Parent Merchant matches check if there is a change in Account Status/Traing Ind fields and update it on the Account.

        for (General_Request__c gr: grAcc.keyset() ){
            for (Account a : acclist){
                if(gr.Merchant__c == a.Merchant_MID__c || gr.Parent_MID__c == a.Merchant_MID__c){
                   accupdt = false;
                   if (gr.perkaStatus__c != '' && gr.perkaStatus__c != null && a.Perka_Account_Status__c != gr.perkaStatus__c){
                        a.Perka_Account_Status__c = gr.perkaStatus__c;
                         accupdt = true;
                     }                    
                  if(gr.Channel_ID__c != null && gr.Channel_ID__c != '' && gr.Channel_ID__c.contains('ISO') && gr.Perka_Training_Indicator__c != ''                      && gr.Perka_Training_Indicator__c != null && (a.Perka_Training_Indicator__c != gr.Perka_Training_Indicator__c)){
                        a.Perka_Training_Indicator__c = gr.Perka_Training_Indicator__c;   
                        accupdt = true;
                  }
                }  
                if(accupdt){
                    accUpdlist.add(a);
                }                
            }                  
        }//End of For Loop                  
Best Answer chosen by SivaG
lakslaks

Hi Kumar,

Although the declaration part is not included above, I suspect you have not initialized the Boolean field accupdt during declaration.

So in the case where control doesn't enter either of the if loops that set the value to True/False, it throws a NullPointerException when the variable is accessed in the line if(accupdt)

You can set a default value (True/False) which is suitable for your requirement during declaration so that the NullPointer isn't thrown.

Regards,
Lakshmi.

All Answers

lakslaks

Hi Kumar,

Although the declaration part is not included above, I suspect you have not initialized the Boolean field accupdt during declaration.

So in the case where control doesn't enter either of the if loops that set the value to True/False, it throws a NullPointerException when the variable is accessed in the line if(accupdt)

You can set a default value (True/False) which is suitable for your requirement during declaration so that the NullPointer isn't thrown.

Regards,
Lakshmi.
This was selected as the best answer
SivaGSivaG
Hi Lakshmi,

You are correct. Its not initialized during declaration. Please see below my Updated code and let me know if something is wrong.

1 question in line 'if(!accUpdlist.isEmpty())' if accUpdlist is empty will it throw any List exception or null pointer exception?

Boolean accupdt;
        List<Account> accUpdlist = new List<Account>();
        List<Account> acclist = new List<Account>([Select Id,Merchant_MID__c,Perka_Account_Status__c,Perka_Training_Indicator__c
                                                  FROM Account WHERE Id IN : grAcc.values()]);
        for (General_Request__c gr: grAcc.keyset() ){
            for (Account a : acclist){
                if(gr.Merchant__c == a.Merchant_MID__c || gr.Parent_MID__c == a.Merchant_MID__c){
                   accupdt = false;
                   if (gr.perkaStatus__c != '' && gr.perkaStatus__c != null && a.Perka_Account_Status__c != gr.perkaStatus__c){
                        a.Perka_Account_Status__c = gr.perkaStatus__c;
                         accupdt = true;
                     }                    
                  if(gr.Channel_ID__c != null && gr.Channel_ID__c != '' && gr.Channel_ID__c.contains('ISO') && gr.Perka_Training_Indicator__c != '' &&
                     gr.Perka_Training_Indicator__c != null && (a.Perka_Training_Indicator__c != gr.Perka_Training_Indicator__c)){
                        a.Perka_Training_Indicator__c = gr.Perka_Training_Indicator__c;   
                        accupdt = true;
                  }
                  if(accupdt){
                    accUpdlist.add(a);
                  }                               
                }       
            }                  
        }//End of For Loop                  
        if(!accUpdlist.isEmpty()){    
            try {
                 //this statement updates accounts
                 update accUpdlist;                    
                }
            catch (System.DmlException de) {
                    System.debug('Exception Occurred on Account Update '+ de);
             }
        }

Thanks
Kumar
lakslaks
Hi Kumar,

accUpdlist.isEmpty() is not going to throw any List exception or null pointer exception as long as you have initialized it as in 
List<Account> accUpdlist = new List<Account>();

If the list is empty it will return True, else False.

You can check the List methods here
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_list.htm#apex_System_List_methods

Regards,
Lakshmi.


 
SivaGSivaG
Thanks Lakshmi. I thought so.
lakslaks
Happy to be of help. :-)

Regards,
Lakshmi.