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
Livia PimentelLivia Pimentel 

Use Apex Class on Trigger [NullPointerException Problem]

Hello, everyone.

I am developing a trigger which has a complicated logic. To deal with this complexity, I am using 3 different Apex classes inside my trigger in the following way:

TRIGGER: 
 
trigger total_rfq_proposta on RFQ__c (after update) {
  
        if (Trigger.isAfter) {
        if (Trigger.isUpdate) {
            
           //HelloWorld.helloWorld2( Trigger.new );
              
           RFQHandler.updateRFQ(Trigger.new);                   
        }            
}
             
}

RFQHandler CLASS:
 
public class RFQHandler{



public static double assignMonthVolumes(integer indice_ano, integer flag_month , RFQ__c rfq) {} ....


public static double getMonthlyVolumes(integer year, integer indice_ano ,RFQ__c rfq){}...


public static double getYearlyVolumes(integer year, integer indice_ano ,RFQ__c rfq){}...


// This is the "main" function
  
public static void updateRFQ(List <RFQ__c> rfqs){
        
        
        for(RFQ__c rfq : rfqs){
            
            if(rfq!=null){
                
                
           
            
            
     Proposta__c proposal = [SELECT Id, Total_RFQ_MWm__c	from Proposta__c WHERE RFQ__c=:rfq.id];
        
     if(proposal != null){
            
            double total_2022_MWm = 0;
            double total_2023_MWm = 0;
            double total_2024_MWm = 0;
            double total_2025_MWm = 0;
            double total_2026_MWm = 0;
            double total_2027_MWm = 0;
            
            // Lista que guardará todos os anos referentes à RFQ           
            list <integer> years = new list <integer>(); 
            
            Date d1 = rfq.In_cio_Suprimento__c;
            
            
            // ex.: inicio = novembro de 2022 || fim = julho de 2024
            while(d1.year() <= rfq.Validade_Suprimento__c.year()){
                
                years.add(d1.Year());    
                d1 = d1.addYears(1);
                
                
            }
            
            // Dicionário que aponta quais anos correspondem a Ano1/Ano2/Ano3 ... etc
          	map <String, Integer> mapa_anos = new map <String, Integer>(); 
            
            // Mapeando os anos correspondentes à RFQ em questão:
            // years = lista contendo todos os anos (integers)
            for(integer value : years){
                
                integer indice = years.indexof(value);
                
                switch on indice{
                    
                    when(0){
                        
                        mapa_anos.put('Ano1',value);
                        
                    }when(1){
                        
                        mapa_anos.put('Ano2',value);
                        
                    }
                    when(2){
                        
                        mapa_anos.put('Ano3',value);
                        
                    }
                    when(3){
                        
                        mapa_anos.put('Ano4',value);
                        
                    }when(4){
                        
                        mapa_anos.put('Ano5',value);
                        
                    }when(5){
                        
                        mapa_anos.put('Ano6',value);
                        
                    }              
                    
                    
                    
                }
                               
                                   
            }
          								          
     
            integer count = 0;
                
            for(integer ano : mapa_anos.values()){
                                                        
                 // Para cada string "Ano1 / Ano2 / Ano3 ...."
                 // Verifica qual que é o ano correspondente e atribui os volumes de acordo com a posição deles (Ano1/Ano2/Ano3 ...)  
                                       
                    integer indice_ano = mapa_anos.values().indexof(ano); // Índice do ano correspondente
                
                	system.debug(indice_ano);
                
                	system.debug(rfq.Name);
                    
                    switch on ano{
                        
                        when 2022{
                           
                           double valor_anual =  getYearlyVolumes(2022,indice_ano, rfq);                        
                           double valor_mensal = getMonthlyVolumes(2022, indice_ano, rfq);
          					
                            if(valor_anual != 0){
                                
                                proposal.Total_RFQ_MWm__c = valor_anual;                               
                                update proposal;
                                                        
                            }else{
                            
                                proposal.Total_RFQ_MWm__c = valor_mensal;                         
                                update proposal;
                                
                            }
                            
                        }
                        
                        when 2023{
                            
                            
                            
                            
                        }
                        
                        
                        when 2024{
                            
                            
                            
                            
                        }when 2025{
                            
                            
                            
                        }when 2026{
                            
                            
                            
                        }when 2027{
                            
                            
                            
                        }
                        
                        
                        
                    }                  
                   
                                       
                    
                    
                }
          
                                       
                
     
            
            
        }
        
    
            }
            
            
        }
        
        
        
    }
    
    


}

OBS: I am not posting all the classes, otherwise this post would be too extensive.


PROBLEM:

In this scenario, I am having the  following problem when testing my Trigger:

Inside RFQHandler --> void updateRFQ, when calling the functions getYearlyVolumes and getMonthlyVolumes like shown below
 
double valor_anual =  getYearlyVolumes(2022,indice_ano, rfq);                 
double valor_mensal = getMonthlyVolumes(2022, indice_ano, rfq);

The following error messages appear:

System.DmlException: Update failed. First exception on row 0 with id a05HZ0000001mdhYAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, total_rfq_proposta: execution of AfterUpdate

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

Class.RFQHandler.getYearlyVolumes: line 958, column 1
Class.RFQHandler.updateRFQ: line 1196, column 1
Trigger.total_rfq_proposta: line 8, column 1: []


I am having trouble figuring out why this is happening.

Any help would be deeply appreciated.

Thank you,

Lívia.
Best Answer chosen by Livia Pimentel
SwethaSwetha (Salesforce Developers) 
volume_ano = 12*double.valueOf(rfq.Volume_Ano1MWm__c.replace(‘,’,‘.’))/horas_ano;

Prior to the above line, can you check what is the value being returned for rfq.Volume_Ano1MWm__c and horas_ano by adding system.debug statements.

Also, you are checking rfq as null but not the field's values. The object will return but fields values could be null. Thanks

All Answers

SwethaSwetha (Salesforce Developers) 
Hi Livia,

You may need to check the null values where ever you are using functions like as below.For the below lines.
 
Date d1 = rfq.In_cio_Suprimento__c;
while(d1.year() <= rfq.Validade_Suprimento__c.year()){
                
                years.add(d1.Year());    
                d1 = d1.addYears(1);
                
                
            }
There may be a chance that d1 is null and you are using d1.year() which may cause the issue. Similarly for rfq.Validade_Suprimento__c.year() as well. If rfq.Validade_Suprimento__c is null then it may throw the error.So where ever you are using functions ite better to check if that field is not equal to null.

If this information helps, please mark the answer as best. Thank you
Livia PimentelLivia Pimentel
Hello, Swetha.

Thank you for your answer. I've just added a check to address the possibility of having a null date, but the problem is still happening. It happens on the following line:

 volume_ano =  12*double.valueOf(rfq.Volume_Ano1MWm__c.replace(',','.'))/horas_ano;

This line is inside an if statement that checks if the Volume_Ano1MWm__c is null (so it is guaranteed that it is not null). Also, this line is inside a function which is called in the "main" class void updateRFQ(List <RFQ__c> rfqs). This class uses a list of objects RFQ__c, which is called in the trigger as Trigger.New.

Altough I am checking if the rfq object is not null, this error message still appears, I don't know why. :( 
 
SwethaSwetha (Salesforce Developers) 
volume_ano = 12*double.valueOf(rfq.Volume_Ano1MWm__c.replace(‘,’,‘.’))/horas_ano;

Prior to the above line, can you check what is the value being returned for rfq.Volume_Ano1MWm__c and horas_ano by adding system.debug statements.

Also, you are checking rfq as null but not the field's values. The object will return but fields values could be null. Thanks
This was selected as the best answer
Livia PimentelLivia Pimentel
Thank you very much, Swetha. The problem was indeed caused by a null field. It is now working. 

I appreciate your help.

All the best,

Lívia.