0

I'm trying to format the CPFs (character set) from a list, but I'm getting the following error: List index out of bounds: 0 for what I'm researching this error happens because I'm trying to access an index that doesn't exist or is null, does anyone know how to solve this? Comment out the line: Controller:

public with sharing class DadosNotaPromissoriaController {
    public transient DadosNotaPromissoriaVF_VO ntPromissoria  {get; set;}
    public List <OpportunityContactRole> avalista {get;set;}
    public String idOpp {get; set;}
    public List <String> cpfcontact {get; set;}
    public List <String> cpfProcurador {get; set;}
    public Integer bemSize  {get; set;}
 
    public String formatacpf (String cpf){
        return cpf.substring(0, 3) + '.' +
            cpf.substring(3, 6) + '.' +
            cpf.substring(6, 9) + '-' +
            cpf.substring(9, 11);
    }
    
     public List <OpportunityContactRole> getAvalist(){ 
         if(avalista==null){
             avalista=[SELECT Id,contactId, contact.Name,contact.CPF__c,contact.MailingCity,contact.MailingStreet,contact.MailingState,
                       contact.MailingPostalCode, contact.Procurador_do_Contato__r.Name, contact.Procurador_do_Contato__r.CPF__c
                       FROM OpportunityContactRole 
                       WHERE OpportunityId =: idOpp
                      ];
         }  
        bemSize = avalista.size();//Pego a quantidade de avalistas
      
        cpfcontact = new List<String>();  
        Integer j;
        j=0; 
        for(OpportunityContactRole i: avalista){
           cpfcontact[j]=formatacpf(avalista[j].contact.CPF__c);
           System.debug('avalista[j].contact.CPF__c' + avalista[j].contact.CPF__c); 
           System.debug('j' + j); 
           j++;
           }
        return avalista;
        }
0

1 Answer 1

5

Your issue is with the way you've written your code.

cpfcontact = new List<String>();  
Integer j;
j=0; 
for(OpportunityContactRole i: avalista){
    cpfcontact[j]=formatacpf(avalista[j].contact.CPF__c);
    System.debug('avalista[j].contact.CPF__c' + avalista[j].contact.CPF__c); 
    System.debug('j' + j); 
    j++;
}

The issue you had in your previous question (where cpfcontact was never initialized, and thus null) was only part of the issue. Your list is no longer null, but it is still empty.

You're trying to add something to your list, but cpfcontact[j] = <some value>; isn't how you do that. Instead, you want to call the .add() method. There is a version of that method where you can tell it to add the item at a certain index in your list, but it doesn't look like you need to do that.

Since it really doesn't have any use in your code (as written), you can get rid of your j variable entirely.

cpfcontact = new List<String>();

// This type of for loop iterates over everything in avalista one at a time, and
//   makes it available inside your loop through the variable 'i'.
// There's no need to do something like 'avalista[j]' here
// The way you had things before, i and avalista[j] would be exactly the same
for(OpportunityContactRole i: avalista){
    // If you want to use a value that you're putting into a list (or a set, or a map)
    //   elsewhere, it makes sense to store that information in a variable
    String formattedCPF = formatCPF(i.Contact.CPF__c);
    cpfcontact.add(formattedCPF);
    
    System.debug('i.contact.CPF__c ' + i.contact.CPF__c); 
    System.debug('formatted CPF: ' + formattedCPF);
}
1
  • Thank you for your help, for your patience, and for the class... really thank you @DerekF
    – Souza
    Mar 9, 2022 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .