+ Start a Discussion
QD93QD93 

Null Exception: Attempt to De-reference a Null Object

Hi,

I am getting a Null Exception, on line 12-18. The idea here is that if that I am trying to only have my Task's related to ID field = Opportunity. So when I relate the Task to an Account the trigger shouldn't do anything. However, I am getting a null exception in my loop b/c the loop is returning null values. 
 
public class LFGEmailMessageClass {

    public static void InsertTask (list<Task> NewTasks) {
    
    List<Task> tasks       = new List<Task>();
    List<Task> Updatetasks = new List<Task>();
    Set<Id> opportunityIds = new Set<Id>();
    Set<Id> taskIds        = new Set<Id>();
    String Y               = '006';
     
     
     for (Task task: newtasks){  
     if (Task != Null &&
     !((string)task.WhatId).startsWith(Y)){                 
                opportunityids = Null;
        }
     Else if (Task != Null &&
     ((string)task.WhatId).startsWith(Y)){
     
                 opportunityIds.add(task.WhatId);
                taskIds.add(task.Id);   
        }}
       

        Map<Id, Id> opportunities = new Map<Id, Id> ();
        
       if(opportunityids != Null){ 
        for (Opportunity opportunity: [SELECT Id, Name, accountid, company__r.Id FROM Opportunity WHERE Id IN :opportunityIds AND accountid = null]) { 
          
            opportunities.put(opportunity.id, opportunity.company__r.Id);
            } 
            }
        
        Map<Id, Id> TasksMap = new Map <Id, Id> ();
        
        For (Task taskss: [SELECT Id, WhatId FROM Task WHERE Id IN :taskIds]){
            TasksMap.put(taskss.Id,Taskss.WhatId);}

        for (Task task: newTasks) {
            if (task.TaskSubType == 'Email' && 
                task.Status      == 'Completed' &&
                task.WhoId       != Null         &&
                task.WhatId      != Null &&
                ((string)task.WhatId).startsWith(Y)) {
                     
                    Task updateTask     = new Task();
                    updateTask.ID       = task.Id;
                    updateTask.WhatId   = task.WhatId;
                    tasks.add(updateTask);
               
                }
            Else IF (task.TaskSubType == 'Email' && 
                task.Status      == 'Completed' &&
                task.WhoId       != Null         &&
                task.WhatId      != Null &&
                !((string)task.WhatId).startsWith(Y)) {
                     
                    Task updateTask     = new Task();
                    updateTask.ID       = task.Id;
                    updateTask.WhatId   = task.WhatId;
                    UpdateTasks.add(updateTask);  
            } 
            }
            {if(tasks.size()>0){            
             update tasks; } 
            
            Else IF (updateTasks.size()>0){
            update updateTasks;}}
             
            System.debug( Tasks);        
     }

 
Rahul.MishraRahul.Mishra
Hi,

Replace your code 12-22 with following code:
 
if(!NewTasks.isEmpty() { 	
		for(Task tsk : newtasks) {
		  if(tsk.WhatId.startsWith('006') {
		     opportunityIds.add(task.WhatId);
             taskIds.add(task.Id);
		  }
		
		}
      }

I hope you will not get the exception after that.

Mark solved if it does help you.​
QD93QD93
Hi Rahul,

Unfortunately, I was hit with this error message -->

 Compile Error: Method does not exist or incorrect signature: void startsWith(String) from the type Id 
QD93QD93
Hi, 

Now I am getting the error message:  Compile Error: Method does not exist or incorrect signature: void add(Schema.SObjectField) from the type Set<Id> 

for the line --> opportunity.ids.add(task.whatid);
Rahul.MishraRahul.Mishra
Hi,

Please copy paste below code and test if it works fine:
 
public class LFGEmailMessageClass {

    public static void InsertTask (list<Task> NewTasks) {
    
    List<Task> tasks       = new List<Task>();
    List<Task> Updatetasks = new List<Task>();
    Set<Id> opportunityIds = new Set<Id>();
    Set<Id> taskIds        = new Set<Id>();
    String Y               = '006';
     
     
        if(!NewTasks.isEmpty()) { 	
		for(Task tsk : newtasks) {
                  
          String strOpp = tsk.WhatId;
		  if(strOpp.startsWith('006')) {
		      
		     opportunityIds.add(tsk.WhatId);
             taskIds.add(tsk.Id);
		  }
		
	    }
      }
       

        Map<Id, Id> opportunities = new Map<Id, Id> ();
        
       if(opportunityids != Null){ 
        for (Opportunity opportunity: [SELECT Id, Name, accountid, company__r.Id FROM Opportunity WHERE Id IN :opportunityIds AND accountid = null]) { 
          
             opportunities.put(opportunity.id, opportunity.company__r.Id);
            } 
            }
        
        Map<Id, Id> TasksMap = new Map <Id, Id> ();
        
        For (Task taskss: [SELECT Id, WhatId FROM Task WHERE Id IN :taskIds]){
            TasksMap.put(taskss.Id,Taskss.WhatId);}

        for (Task task: newTasks) {
            if (task.TaskSubType == 'Email' && 
                task.Status      == 'Completed' &&
                task.WhoId       != Null         &&
                task.WhatId      != Null &&
                ((string)task.WhatId).startsWith(Y)) {
                     
                    Task updateTask     = new Task();
                    updateTask.ID       = task.Id;
                    updateTask.WhatId   = task.WhatId;
                    tasks.add(updateTask);
               
                }
            Else IF (task.TaskSubType == 'Email' && 
                task.Status      == 'Completed' &&
                task.WhoId       != Null         &&
                task.WhatId      != Null &&
                !((string)task.WhatId).startsWith(Y)) {
                     
                    Task updateTask     = new Task();
                    updateTask.ID       = task.Id;
                    updateTask.WhatId   = task.WhatId;
                    UpdateTasks.add(updateTask);  
            } 
            }
            {if(tasks.size()>0){            
             update tasks; } 
            
            Else IF (updateTasks.size()>0){
            update updateTasks;}}
             
            System.debug( Tasks);        
     }
}

Mark solved if does help you.
QD93QD93
Hi, I am able to save the code but still receiving the same issue as before "Null Exception: Deference a Null Object":User-added image