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
Nikhil Khandare 1Nikhil Khandare 1 

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

HI All,

 I am executing below code. 

global class CustomIterable implements Iterator<Object>{ 
   List<Account> accs {get; set;} 
   List<Object> listOfAccounts {get; set;}
   Integer i {get; set;} 
   global CustomIterable(){ 
       string s = '{"d":[{"Sede":"Crai","Cedi":"A.R.Ce.V","CodECR":"0010001","IDPV":1,"IDNielsen":"","CodAgente":"","RagSoc":"Punto vend. 1","PartitaIVA":""},{"Sede":"Crai","Cedi":"A.R.Ce.V","CodECR":"0010001","IDPV":1,"IDNielsen":"","CodAgente":"","RagSoc":"Punto vend. 1","PartitaIVA":""}]}';
       Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
       List<Object> listOfAccounts = (List<Object>) m.get('d');
       System.debug('Size Of Account oBjects:' + listOfAccounts.size());
       accs = [SELECT id, name, numberofEmployees FROM Account LIMIT 10]; 
       //System.debug('size of Total Account List:' + accs.size());
       i = 0; 
   }   
   global boolean hasNext(){ 
       System.debug('Size of ListOfAccounts:' + listOfAccounts.size());
       if(i >= listOfAccounts.size()) 
           return false; 
       else 
           return true; 
   }    
   global Object next(){ 
       i=i+1; 
       return listOfAccounts[i-1]; 
   } 
}

I am getting the below error on line System.debug('Size of ListOfAccounts:' + listOfAccounts.size());
FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

I cab get the size of list listOfAccounts in cinstructor. However, I am getting the error on mentioned line.

Please help me resolve this query.

Thanks,
Nikhil Khandare
Bhanu MaheshBhanu Mahesh
Hi Nikhil,

You are declaring a new variable listOfAccounts in constructor.
Instaed of thet iniatialize the List listOfAccounts and assign values to it.

As the list variable is not declared, you are geetting this error.

Instead of List<Object> listOfAccounts = (List<Object>) m.get('d'); in contsructor initialize the variable and assign values
listOfAccounts = new List<Object>();
listOfAccounts = (List<Object>) m.get('d');

Use the below code
 
global class CustomIterable implements Iterator<Object>{ 
   List<Account> accs {get; set;} 
   List<Object> listOfAccounts {get; set;}
   Integer i {get; set;} 
   global CustomIterable(){ 
       string s = '{"d":[{"Sede":"Crai","Cedi":"A.R.Ce.V","CodECR":"0010001","IDPV":1,"IDNielsen":"","CodAgente":"","RagSoc":"Punto vend. 1","PartitaIVA":""},{"Sede":"Crai","Cedi":"A.R.Ce.V","CodECR":"0010001","IDPV":1,"IDNielsen":"","CodAgente":"","RagSoc":"Punto vend. 1","PartitaIVA":""}]}';
       Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
       listOfAccounts  = new List<Object>();
       listOfAccounts = (List<Object>) m.get('d');
       System.debug('Size Of Account oBjects:' + listOfAccounts.size());
       accs = [SELECT id, name, numberofEmployees FROM Account LIMIT 10]; 
       //System.debug('size of Total Account List:' + accs.size());
       i = 0; 
   }   
   global boolean hasNext(){ 
       System.debug('Size of ListOfAccounts:' + listOfAccounts.size());
       if(i >= listOfAccounts.size()) 
           return false; 
       else 
           return true; 
   }    
   global Object next(){ 
       i=i+1; 
       return listOfAccounts[i-1]; 
   } 
}

Thanks,
Bhanu Mahesh Gadi
Nikhil Khandare 1Nikhil Khandare 1
Hi Bhanu,

Thanks for the reply. Yes, I realized that that error just after posting this error. Anyway, Thank you for ur reply