+ Start a Discussion
Shawn Reichner 29Shawn Reichner 29 

Null Pointer Exception.....Help Please!!!

Hello awesome developers!

I have a trigger that I created that should fire to rollup the count of Tasks assoctiated with a Lead in our ORG.  The trigger works as it should and the rollups are perfrorming as we want, however on all other objects (standard or custom) when a Task is being created we receive the following error. 

Apex trigger LeadCountOfTasks caused an unexpected exception, contact your administrator: LeadCountOfTasks: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.LeadCountOfTasks: line 11, column 1

I am seeign that the whoId is not being populated on Account Tasks as that is the WhatId and I think that is the issue with the error, but I am new to apex so I am not sure.  As you can see from my following code, I attemted on the line where this error is coming from, to exclude all tasks for objects other than leads, so again I am lost and lookign for some help to fix this one.  

Thank you in advance fo ryour help and I know one of you awesome Devs can help me with this one!

Shawn
 
trigger LeadCountOfTasks on Task (after delete, after insert, after undelete, after update) {
    Task [] tlist;
        if(Trigger.isDelete)
            tlist= Trigger.old;
        else
            tlist = trigger.new;
    set<id> lid = new set< id> ();
    
    If(!tlist.isEmpty()){
    for(Task ts : tlist)
    {   if(string.valueof(ts.WhoId).startswith('00Q') && ts.WhoId!=null && ts.whatId == null)
        lid.add(ts.Whoid);
    } 
    If(lid.size()>0){
    Map <id,Task > tmap = new Map <id, Task>([select id, Whoid from Task where Whoid in:lid]);
    Map <id, Lead> lmap = new Map <id, Lead>([select id,Count_Activity__c from lead where id in:lid ]);
    
        If(lmap.size()>0){
       List<Lead> llist = [SELECT Id, Count_Activity__c FROM Lead WHERE ID IN:lmap.values()]; 
    for(lead l : lmap.values())
    {
        set<id> tids = new set <id>();
        for(Task tt : tmap.values())
        {
           if(tt.WhoId== l.Id)
            tids.add(tt.id);
        }
        if(l.Count_Activity__c!=tids.size())
        l.Count_Activity__c=tids.size();
        
              tlist = [select id, Whoid from task where whoid in:lid ];
        for(lead le :llist)
        {
            for(Task te:tlist)
            {
                if(tlist.size()>0)
                le.Count_Activity__c = tlist.size();
            }
        }
        
        
    }
    If(lmap.size()>0){
    update lmap.values();
    }
    }
    }
    }
}

 
Best Answer chosen by Shawn Reichner 29
Nithesh NNithesh N
Lets pull the Object Check from the If condition and Nest it inside. 
trigger LeadCountOfTasks on Task (after delete, after insert, after undelete, after update) {
    Task [] tlist;
        if(Trigger.isDelete)
            tlist= Trigger.old;
        else
            tlist = trigger.new;
    set<id> lid = new set< id> ();
    
    If(!tlist.isEmpty()){
    for(Task ts : tlist)
    {   
          if( ts.WhoId != null && ts.whatId == null) 
          {
             if(string.valueof(ts.WhoId).startswith('00Q')) 
             {
                  lid.add(ts.Whoid);
             }
             
          }
        
    } 
    If(lid.size()>0){
    Map <id,Task > tmap = new Map <id, Task>([select id, Whoid from Task where Whoid in:lid]);
    Map <id, Lead> lmap = new Map <id, Lead>([select id,Count_Activity__c from lead where id in:lid ]);
    
        If(lmap.size()>0){
       List<Lead> llist = [SELECT Id, Count_Activity__c FROM Lead WHERE ID IN:lmap.values()]; 
    for(lead l : lmap.values())
    {
        set<id> tids = new set <id>();
        for(Task tt : tmap.values())
        {
           if(tt.WhoId== l.Id)
            tids.add(tt.id);
        }
        if(l.Count_Activity__c!=tids.size())
        l.Count_Activity__c=tids.size();
        
              tlist = [select id, Whoid from task where whoid in:lid ];
        for(lead le :llist)
        {
            for(Task te:tlist)
            {
                if(tlist.size()>0)
                le.Count_Activity__c = tlist.size();
            }
        }
        
        
    }
    If(lmap.size()>0){
    update lmap.values();
    }
    }
    }
    }
}

Let me know if it works. 

Best,
Nithesh

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
trigger LeadCountOfTasks on Task (after delete, after insert, after undelete, after update) {
    Task [] tlist;
        if(Trigger.isDelete)
            tlist= Trigger.old;
        else
            tlist = trigger.new;
    set<id> lid = new set< id> ();
    
    If(!tlist.isEmpty()){
    for(Task ts : tlist)
    {   if(ts.WhoId!=null && ts.whatId == null && string.valueof(ts.WhoId).startswith('00Q') )
        lid.add(ts.Whoid);
    } 
    If(lid.size()>0){
    Map <id,Task > tmap = new Map <id, Task>([select id, Whoid from Task where Whoid in:lid]);
    Map <id, Lead> lmap = new Map <id, Lead>([select id,Count_Activity__c from lead where id in:lid ]);
    
        If(lmap.size()>0){
       List<Lead> llist = [SELECT Id, Count_Activity__c FROM Lead WHERE ID IN:lmap.values()]; 
    for(lead l : lmap.values())
    {
        set<id> tids = new set <id>();
        for(Task tt : tmap.values())
        {
           if(tt.WhoId== l.Id)
            tids.add(tt.id);
        }
        if(l.Count_Activity__c!=tids.size())
        l.Count_Activity__c=tids.size();
        
              tlist = [select id, Whoid from task where whoid in:lid ];
        for(lead le :llist)
        {
            for(Task te:tlist)
            {
                if(tlist.size()>0)
                le.Count_Activity__c = tlist.size();
            }
        }
        
        
    }
    If(lmap.size()>0){
    update lmap.values();
    }
    }
    }
    }
}

Let us know if this will help you
 
Nithesh NNithesh N
Lets pull the Object Check from the If condition and Nest it inside. 
trigger LeadCountOfTasks on Task (after delete, after insert, after undelete, after update) {
    Task [] tlist;
        if(Trigger.isDelete)
            tlist= Trigger.old;
        else
            tlist = trigger.new;
    set<id> lid = new set< id> ();
    
    If(!tlist.isEmpty()){
    for(Task ts : tlist)
    {   
          if( ts.WhoId != null && ts.whatId == null) 
          {
             if(string.valueof(ts.WhoId).startswith('00Q')) 
             {
                  lid.add(ts.Whoid);
             }
             
          }
        
    } 
    If(lid.size()>0){
    Map <id,Task > tmap = new Map <id, Task>([select id, Whoid from Task where Whoid in:lid]);
    Map <id, Lead> lmap = new Map <id, Lead>([select id,Count_Activity__c from lead where id in:lid ]);
    
        If(lmap.size()>0){
       List<Lead> llist = [SELECT Id, Count_Activity__c FROM Lead WHERE ID IN:lmap.values()]; 
    for(lead l : lmap.values())
    {
        set<id> tids = new set <id>();
        for(Task tt : tmap.values())
        {
           if(tt.WhoId== l.Id)
            tids.add(tt.id);
        }
        if(l.Count_Activity__c!=tids.size())
        l.Count_Activity__c=tids.size();
        
              tlist = [select id, Whoid from task where whoid in:lid ];
        for(lead le :llist)
        {
            for(Task te:tlist)
            {
                if(tlist.size()>0)
                le.Count_Activity__c = tlist.size();
            }
        }
        
        
    }
    If(lmap.size()>0){
    update lmap.values();
    }
    }
    }
    }
}

Let me know if it works. 

Best,
Nithesh
This was selected as the best answer
GauravGargGauravGarg
Hi Shawn,

Please replace line 11 with below line:
if(ts.WhoId!=null && ts.whatId == null && string.valueof(ts.WhoId).startswith('00Q') )

Hope this help!!! Let me know if you still face the error.

Thanks,
Gaurav 
Shawn Reichner 29Shawn Reichner 29
Thank you all!!!!!  Issue fixed!