+ Start a Discussion
UrvikUrvik 

Attempt to de-reference a null object

I am getting an error "execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Class.DecalsController.OnAfterUpdate: line 39, column 1".. Below is my controller. Could someone please help?

 

Controller:

public with sharing class DecalsController {
    public DecalsController(boolean triggerIsExecuting, integer size){
    }
    public void OnAfterInsert(Payment__c[] newPayments){
        
      try{      
        For(Payment__c pa:newPayments)
        {   
        system.debug('**********id*********************'+pa.Decal__c);
         List<Payment__c> b = [select id ,Decal__c,Amount_Paid__c from Payment__c where Decal__c=:pa.Decal__c];    
         system.debug('**********payments*********************'+b);
          Decals__c dcc   = new Decals__c();
          dcc.id        = pa.Decal__c;
          Double A=0;
          for(Payment__c py : b){
        
          A= A + Integer.valueOf(py.amount_paid__c);
          dcc.Total_Amount_Paid__c = A;
          system.debug('*************Balance****************************'+A);
          }
          update dcc;
            }
      }catch(DMLexception e){}      
     
    }
    public void OnAfterUpdate(Payment__c[]oldpayments, payment__c[] updatePayments){
        
      try{      
        For(Payment__c pa:updatePayments)
        {   
        system.debug('**********id*********************'+pa.Decal__c);
         List<Payment__c> b = [select id ,Decal__c,Amount_Paid__c from Payment__c where Decal__c=:pa.Decal__c];
        
          Decals__c dcc   = new Decals__c();
          dcc.id        = pa.Decal__c;
          Double A=0;
          for(Payment__c py : b){
         
          A= A + Integer.valueof(py.amount_paid__c);
          dcc.Total_Amount_Paid__c = A;
         
          }
          update dcc;
            }
      }catch(DMLexception e){}      
     
    }
    }

 Trigger

trigger DecalsTrigger on Payment__c (after insert,after update) {

 DecalsController handler = new DecalsController(Trigger.isExecuting, Trigger.size);

 if(Trigger.isInsert && Trigger.isAfter){
 handler.OnAfterInsert(Trigger.new);
 }
 else if(Trigger.isUpdate && Trigger.isAfter){
 handler.OnAfterUpdate(Trigger.old, Trigger.new);
 }
 }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
imutsavimutsav
Add try catch on this line. This should resolve this error.

A= A + Integer.valueof(py.amount_paid__c);

To

try {
A= A + Integer.valueof(py.amount_paid__c);
} catch (Exception e) {
System.debug('---------Exception ------- ' + e );
}

Set your user to the debug log. Execute this program and see what type of error you are getting. This would help to resolve this issue. I guess some of your records in Payment__c table don't have any value in amount_paid__c. And thats the reason when you are trying to convert it to Integer then you are getting this error.

Thanks
Utsav

[Do mark this answer as solution and give Kudos if this helps you resolve the issue]

All Answers

imutsavimutsav
Add try catch on this line. This should resolve this error.

A= A + Integer.valueof(py.amount_paid__c);

To

try {
A= A + Integer.valueof(py.amount_paid__c);
} catch (Exception e) {
System.debug('---------Exception ------- ' + e );
}

Set your user to the debug log. Execute this program and see what type of error you are getting. This would help to resolve this issue. I guess some of your records in Payment__c table don't have any value in amount_paid__c. And thats the reason when you are trying to convert it to Integer then you are getting this error.

Thanks
Utsav

[Do mark this answer as solution and give Kudos if this helps you resolve the issue]
This was selected as the best answer
nbknbk

HI,

 

can you please add the below highlited condition in your code.

for(Payment__c py : b){
         if(py.amount_paid__c!=null || py.amount_paid__c!='')

        {
             A= A + Integer.valueOf(py.amount_paid__c);
             dcc.Total_Amount_Paid__c = A;
         }
         

 

}

Suresh RaghuramSuresh Raghuram

do like this

 

where ever the line number it is showing find out is there any value you getting after query.

 

and passing that to some other or doing comparision just make a null check.

suppose.

string[] s = [select NAme from Lead where id='sonme thing'];

string j;

if(s != '' || s!= null){

j=s;

}

finally what i would like to say is do the null check, when u such kind of error.

 

If this answers your question make this as a solution and give KUDOS.

UrvikUrvik

Thank you so much to imutsav, nbk and suree.