I am getting runtime error with null pointer exception.In the debug logs it is showing value of idofacc is null. Please help its urgent.
Batch Apex:
global class testbatch implements Database.Batchable<SObject>, Database.Stateful {
global List<Id> idofacc;
global Account[] updates;
global testbatch(){
List<Id> idofacc = new List<Id>();
Account[] updates = new Account[] {};
}
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator([select Id from Account order by Name]);
}
public void execute(Database.BatchableContext context, List<Account> scope) {
for (AggregateResult ar : [
select Account__c a, count(Id) c
from Contact_Point__c
where Status__c=true and Account__c in :scope
group by Account__c
]) {
set<id> ids=new set<id>();
for(contact com:[SELECT id,firstname,AccountId FROM Contact where accountid =:(Id)ar.get('a')])
{
ids.add(com.id);
}
Integer dec=[Select count() from Contact_Point__c
where Status__c=true and Contact__c in :ids];
updates.add(new Account(id=(Id)ar.get('a'),No_of_active_CP_on_Account__c =(integer)ar.get('c'),No_of_active_CP_on_Contact__c =dec));
idofacc.add((Id)ar.get('a'));
}
update updates;
System.debug('value of update :::: '+updates);
}
public void finish(Database.BatchableContext context) {
Id templateId = [Select id from EmailTemplate where name=:'test template'].id;
System.debug('value of template ::::: '+templateId);
Map<Id, String> emailMap = new Map<Id, String>();
System.debug('value of ID ::: '+idofacc);
List<Account> ls = [select Id,OwnerId,Name FROM Account where Id in :idofacc];
List<String> ownid = new List<String>();
List<String> accName = new List<String>();
System.debug('value of ID ::: '+ls);
for(Account acc: ls){
ownid.add(acc.OwnerId);
accName.add(acc.Name);
}
System.debug('value of accName ::: '+accName);
System.debug('value of ownid ::: '+ownid);
List<User> mailid = [Select Id,Email FROM User WHERE Id in :ownid];
for(User u :mailid){
emailMap.put(u.Id, u.Email);
}
System.debug('value of emailMap ::: '+mailid);
System.debug('value of emailMap ::: '+emailMap.values());
for(Integer i = 1;i<updates.size();i++){
for(String s :accName){
Account CPonAccount = updates[i];
Decimal abc = CPonAccount.No_of_active_CP_on_Account__c;
Decimal pqr = CPonAccount.No_of_active_CP_on_Contact__c;
//String accId = CPonAccount.Id;
//String AccNm = [SELECT Name FROM Account where Id = : accId];
System.debug('value of CPonAccount ::: '+CPonAccount);
System.debug('value of abc ::: '+abc);
System.debug('value of pqr ::: '+pqr);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(CPonAccount.Id);
mail.setToAddresses(emailMap.values());
mail.setSubject('Updated Count of Contact Points for ' + s);
mail.setSaveAsActivity(false);
mail.setTemplateId(templateId);
mail.setPlainTextBody('Number of active contact points on Account ' + abc +'\n'+'Number of active contact points on Contact '+ pqr);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{mail});
}
}
}
}