+ Start a Discussion
Raja JammulaRaja Jammula 

null pointer exception issue

Hi 

i have a below code i am getting the error:
System.NullPointerException: Attempt to de-reference a null object at line 66

global class Match implements Database.Batchable<sObject>{
    
   global Database.QueryLocator start(Database.BatchableContext BC){
       
       String query = 'SELECT Name, Id, test__c, opportunity__c, test1__c FROM pro__c ';
       
       return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<pro__c> planProjects){
        Map<String,List<task__c>> taskw = new Map<String,List<task__c>>();
        Map<String,List<milest>> milestW = new Map<String,List<milest>>();
        
           List<String> ppIds = new List<String>();
        for(pro__c pp : planProjects){
            ppIds.add(pp.Id);
        }
       
        List<task__c> Tractas = [SELECT Id,Name,Tracker__r.TrP__r.pro__c,Status__c,
                                                        Comments__c,Completed_Date__c
                                                        FROM  task__c
                                                        WHERE Tracker__r.TrP__r.pro__c in :ppIds];
        
        for(task__c tt : Tractas){
            List<task__c> taskl = new List<task__c>();
            if(taskw.containsKey(tt.Tracker__r.TrP__r.pro__c)){
                
                taskl = taskw.get(tt.Tracker__r.TrP__r.pro__c);    
            }
            taskl.add(tt);
            taskw.put(tt.Tracker__r.TrP__r.pro__c,taskl);
        }
        
        List<milest> milest= [SELECT Id,Name,Status__c,Comments__c,Date__c,pro__c 
                                                    FROM milest
                                                    WHERE pro__c in :ppIds];
                                                    
        for(milest milestone : milest){
            
            List<milest> mileList = new List<milest>();
            
            if(milestW.containsKey(milestone.pro__c)){
                
                mileList = milestW.get(milestone.pro__c);
                
            }
            
            mileList.add(milestone);
            milestW.put(milestone.pro__c,mileList);
            
        }
       
        List<task__c> tasksToUpdate = new List<task__c>();
        LIst<MilestoneMapping__c> mappingRecords = MilestoneMapping__c.getall().values();
        Map<String,String> milestoneMap = new Map<String,String>();
        
        for(MilestoneMapping__c mappingRec : mappingRecords){
            milestoneMap.put(mappingRec.Task_Name__c, mappingRec.Milestone_Name__c);
        }
       
        for(String ppId : taskw.keyset()){
            
            for(task__c tt : taskw.get(ppId)){
                String milestoneMappinName = milestoneMap.get(tt.Name);
                
                
           ---->     for(milest mile :milestW.get(ppId) ){
                    
                    if(mile.Name == milestoneMappinName){
                        tt.Status__c = mile.Status__c;
                        tt.Comments__c = mile.Comments__c;
                        tt.Completed_Date__c = mile.Date__c;
                        tasksToUpdate.add(tt);
                    }
                }

            }
        }    
       
        if(!tasksToUpdate.isEmpty()){
           update tasksToUpdate;
        }
    }

can anyone please help me to get rid of this error
Best Answer chosen by Raja Jammula
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- issue is cominge because of nested for loop

Please try to update your code like below
global class Match implements Database.Batchable<sObject>{
    
   global Database.QueryLocator start(Database.BatchableContext BC){
       
       String query = 'SELECT Name, Id, test__c, opportunity__c, test1__c FROM pro__c ';
       
       return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<pro__c> planProjects){
        Map<String,List<task__c>> taskw = new Map<String,List<task__c>>();
        Map<String,List<milest>> milestW = new Map<String,List<milest>>();
        
           List<String> ppIds = new List<String>();
        for(pro__c pp : planProjects){
            ppIds.add(pp.Id);
        }
       
        List<task__c> Tractas = [SELECT Id,Name,Tracker__r.TrP__r.pro__c,Status__c,
                                                        Comments__c,Completed_Date__c
                                                        FROM  task__c
                                                        WHERE Tracker__r.TrP__r.pro__c in :ppIds];
														
        for(task__c tt : Tractas){
            List<task__c> taskl = new List<task__c>();
            if(taskw.containsKey(tt.Tracker__r.TrP__r.pro__c)){
                taskl = taskw.get(tt.Tracker__r.TrP__r.pro__c);    
            }
            taskl.add(tt);
            taskw.put(tt.Tracker__r.TrP__r.pro__c,taskl);
        }
        
        List<milest> milest= [SELECT Id,Name,Status__c,Comments__c,Date__c,pro__c 
                                                    FROM milest
                                                    WHERE pro__c in :ppIds];

        // Amit:- try to create new Map like below
		
		Map<String,milest> mapNameWiseMilest = new 	Map<String,milest>();
		
        for(milest milestone : milest){
            List<milest> mileList = new List<milest>();
            if(milestW.containsKey(milestone.pro__c)){
                mileList = milestW.get(milestone.pro__c);
            }
            mileList.add(milestone);
            milestW.put(milestone.pro__c,mileList);
			
			mapNameWiseMilest.put(milestone.name , milestone);
        }

		
        List<task__c> tasksToUpdate = new List<task__c>();
        LIst<MilestoneMapping__c> mappingRecords = MilestoneMapping__c.getall().values();
        Map<String,String> milestoneMap = new Map<String,String>();
        
        for(MilestoneMapping__c mappingRec : mappingRecords){
            milestoneMap.put(mappingRec.Task_Name__c, mappingRec.Milestone_Name__c);
        }
       
        for(String ppId : taskw.keyset())
		{
            for(task__c tt : taskw.get(ppId))
			{
                String milestoneMappinName = milestoneMap.get(tt.Name);
				
				if(mapNameWiseMilest.containsKey(milestoneMappinName))
				{
						milest mile = mapNameWiseMilest.get(milestoneMappinName);	
                        tt.Status__c = mile.Status__c;
                        tt.Comments__c = mile.Comments__c;
                        tt.Completed_Date__c = mile.Date__c;
                        tasksToUpdate.add(tt);
                }
            }
        }
       
        if(!tasksToUpdate.isEmpty()){
           update tasksToUpdate;
        }
    }
	
}

 

All Answers

HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
try using the following trigger,

Trigger Code:
 
global class Match implements Database.Batchable<sObject>{
    
   global Database.QueryLocator start(Database.BatchableContext BC){
       
       String query = 'SELECT Name, Id, test__c, opportunity__c, test1__c FROM pro__c ';
       
       return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<pro__c> planProjects){
        Map<String,List<task__c>> taskw = new Map<String,List<task__c>>();
        Map<String,List<milest>> milestW = new Map<String,List<milest>>();
        
        List<milest> allmilestRecords = New List<milest>();
        
           List<String> ppIds = new List<String>();
           
        for(pro__c pp : planProjects)
        {
            ppIds.add(pp.Id);
        }
       
        List<task__c> Tractas = [SELECT Id,Name,Tracker__r.TrP__r.pro__c,Status__c,
                                                        Comments__c,Completed_Date__c
                                                        FROM  task__c
                                                        WHERE Tracker__r.TrP__r.pro__c in :ppIds];
        
        for(task__c tt : Tractas)
        {
            List<task__c> taskl = new List<task__c>();
            
            if(taskw.containsKey(tt.Tracker__r.TrP__r.pro__c))
            {
                
                taskl = taskw.get(tt.Tracker__r.TrP__r.pro__c);    
            }
            taskl.add(tt);
            taskw.put(tt.Tracker__r.TrP__r.pro__c,taskl);
        }
        
        List<milest> milest= [SELECT Id,Name,Status__c,Comments__c,Date__c,pro__c 
                                                    FROM milest
                                                    WHERE pro__c in :ppIds];
                                                    
        for(milest milestone : milest)
        {
            
            List<milest> mileList = new List<milest>();
            
            if(milestW.containsKey(milestone.pro__c))
            {
                
                mileList = milestW.get(milestone.pro__c);
                
                
                
            }
            
            mileList.add(milestone);
            milestW.put(milestone.pro__c,mileList);
            allmilestRecords.add(milestone);
            
        }
       
        List<task__c> tasksToUpdate = new List<task__c>();
        LIst<MilestoneMapping__c> mappingRecords = MilestoneMapping__c.getall().values();
        Map<String,String> milestoneMap = new Map<String,String>();
        
        for(MilestoneMapping__c mappingRec : mappingRecords){
            milestoneMap.put(mappingRec.Task_Name__c, mappingRec.Milestone_Name__c);
        }
       
        for(String ppId : taskw.keyset()){
            
            for(task__c tt : taskw.get(ppId)){
                String milestoneMappinName = milestoneMap.get(tt.Name);
                
                
           ---->     for(milest mile : allmilestRecords){
                    
                    if(mile.Name == milestoneMappinName){
                        tt.Status__c = mile.Status__c;
                        tt.Comments__c = mile.Comments__c;
                        tt.Completed_Date__c = mile.Date__c;
                        tasksToUpdate.add(tt);
                    }
                }

            }
        }    
       
        if(!tasksToUpdate.isEmpty()){
           update tasksToUpdate;
        }
    }
Hope it helps!
 
Raja JammulaRaja Jammula
Thank you after updating and executing batch i am getting below error
First error: Apex CPU time limit exceeded when i run this batch i am getting this error.
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- issue is cominge because of nested for loop

Please try to update your code like below
global class Match implements Database.Batchable<sObject>{
    
   global Database.QueryLocator start(Database.BatchableContext BC){
       
       String query = 'SELECT Name, Id, test__c, opportunity__c, test1__c FROM pro__c ';
       
       return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<pro__c> planProjects){
        Map<String,List<task__c>> taskw = new Map<String,List<task__c>>();
        Map<String,List<milest>> milestW = new Map<String,List<milest>>();
        
           List<String> ppIds = new List<String>();
        for(pro__c pp : planProjects){
            ppIds.add(pp.Id);
        }
       
        List<task__c> Tractas = [SELECT Id,Name,Tracker__r.TrP__r.pro__c,Status__c,
                                                        Comments__c,Completed_Date__c
                                                        FROM  task__c
                                                        WHERE Tracker__r.TrP__r.pro__c in :ppIds];
														
        for(task__c tt : Tractas){
            List<task__c> taskl = new List<task__c>();
            if(taskw.containsKey(tt.Tracker__r.TrP__r.pro__c)){
                taskl = taskw.get(tt.Tracker__r.TrP__r.pro__c);    
            }
            taskl.add(tt);
            taskw.put(tt.Tracker__r.TrP__r.pro__c,taskl);
        }
        
        List<milest> milest= [SELECT Id,Name,Status__c,Comments__c,Date__c,pro__c 
                                                    FROM milest
                                                    WHERE pro__c in :ppIds];

        // Amit:- try to create new Map like below
		
		Map<String,milest> mapNameWiseMilest = new 	Map<String,milest>();
		
        for(milest milestone : milest){
            List<milest> mileList = new List<milest>();
            if(milestW.containsKey(milestone.pro__c)){
                mileList = milestW.get(milestone.pro__c);
            }
            mileList.add(milestone);
            milestW.put(milestone.pro__c,mileList);
			
			mapNameWiseMilest.put(milestone.name , milestone);
        }

		
        List<task__c> tasksToUpdate = new List<task__c>();
        LIst<MilestoneMapping__c> mappingRecords = MilestoneMapping__c.getall().values();
        Map<String,String> milestoneMap = new Map<String,String>();
        
        for(MilestoneMapping__c mappingRec : mappingRecords){
            milestoneMap.put(mappingRec.Task_Name__c, mappingRec.Milestone_Name__c);
        }
       
        for(String ppId : taskw.keyset())
		{
            for(task__c tt : taskw.get(ppId))
			{
                String milestoneMappinName = milestoneMap.get(tt.Name);
				
				if(mapNameWiseMilest.containsKey(milestoneMappinName))
				{
						milest mile = mapNameWiseMilest.get(milestoneMappinName);	
                        tt.Status__c = mile.Status__c;
                        tt.Comments__c = mile.Comments__c;
                        tt.Completed_Date__c = mile.Date__c;
                        tasksToUpdate.add(tt);
                }
            }
        }
       
        if(!tasksToUpdate.isEmpty()){
           update tasksToUpdate;
        }
    }
	
}

 
This was selected as the best answer