+ Start a Discussion
nagasnagas 

trigger error

hi i have written a asynchronous class for a trigger which i giving me an error 

 

Attempt to de-reference a null object

 

I am attaching my code below please let me know if anybody find an error in it.

 

Trigger:

 

 

trigger enddatechild on Territory (before insert,before update) {

List<Id> currentparent = new List<Id>();

for(Territory t :Trigger.New){
   if(t.enddate__c !=null)
   {
     currentparent.add(t.Id);
   }
}

updateTerritory.updatechildterr(currentparent);
}

 

 

Asynchronous Class:

 

 

public class updateTerritory
  {
     @future
     public static void updatechildterr(List<Id> currentparent)
      {
       List <Territory> Tl = new List <Territory>();
       List<Id> MasterTerrList = new List<ID>();
       Map<Id,Territory> child = new Map<Id,territory>();
       boolean endStatus = False;
       
       while (!endStatus)
    {
        Tl = [Select a.id, a.ParentTerritoryId From Territory a  Where a.ParentTerritoryId IN :currentParent];

        If(Tl.size() != 0)
        {
            currentParent.clear();
            for (Integer i = 0; i < Tl.size(); i++)
            {
                Territory T = Tl[i];
                currentParent.add(T.id);
                MasterTerrList.add(T.id);
            }
        }
        else
            endStatus = True;
    }
    
    for(Territory terry:[select id,enddate__c from territory where id IN :masterterrlist and enddate__c = null])
    {
        if(!child.containskey(terry.id))
        {
            child.put(terry.Id,terry);
        }
    }
    if(!child.isempty())
    {
        trigger.new[0].addError('You cannot enddate the parent territory until you enddate your child territories');
    }
    
    }
    }

 the error is showing up at the line i have marked in red color. Its not invoking the future method throwing this error.

 

bob_buzzardbob_buzzard

As you are executing a future method, I would expect the trigger (and associated transaction) to have completed.

 

Thus when you try to dereference the zero'th element of trigger.new, you get a null pointer exception as trigger.new is empty.