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')])
        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));
        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){
         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.setSubject('Updated Count of Contact Points for ' + s);
            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});



1 Answer 1


When declaring variables you can initialize them in the declaration.

You attempted to initialize them in the testbatch() method but you never called it so that variables / properties were always null

change to:

global List<Id> idofacc = New List<ID>();
global Account[] updates = New List<Account>();


global testbatch(){
    List<Id> idofacc = new List<Id>();
    Account[] updates = new Account[] {};
  • Now no runtime error i am getting but now Finish method is not executing i think as i am not getting system.debug logs for log statement added in the finish method.Any idea why i am not getting any finish block logs.Mail is also not being sent.
    – Jack vardy
    Oct 18, 2015 at 16:02
  • Since this is a different question, please mark this as accepted and start a new question along with the steps you are taking to check if it is running or not. Be as specific as possible to get the best answer.
    – Eric
    Oct 18, 2015 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.