function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
john8407john8407 

Help with System.NullPointerException: Attempt to de-reference a null object:

I keep getting Error:Apex trigger UpdateOpportunityCode96Box caused an unexpected exception, contact your administrator: UpdateOpportunityCode96Box: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateOpportunityCode96Box: line 18, column 8

 

Anyone have any ideas? Appreciate any help.

 

Here is my code: 

 

trigger UpdateOpportunityCode96Box on OpportunityLineItem (after update)
{
List<id> oppIds=new List<id>();
for (OpportunityLineItem oli : trigger.new)
{
   if (oli.Magnys_Service_Code_Copy__c == '96')
   oppIds.add(oli.opportunityid);
}
Map<Id, Opportunity> oppsById=new Map<Id, Opportunity>();
List<Opportunity> opps= [select id, Contains_Code_96__c from Opportunity WHERE id in :oppIds];
oppsById.putAll(opps);
List<Opportunity> oppsToUpdate=new List<Opportunity>();
for (OpportunityLineItem oli : trigger.new)
   {   
       Opportunity opp=oppsById.get(oli.opportunityid);
       opp.Contains_Code_96__c = true;
       oppsToUpdate.add(opp);
   }
update oppsToUpdate;
}

trigger UpdateOpportunityCode96Box on OpportunityLineItem (after update){List<id> oppIds=new List<id>();for (OpportunityLineItem oli : trigger.new){   if (oli.Magnys_Service_Code_Copy__c == '96')   oppIds.add(oli.opportunityid);}Map<Id, Opportunity> oppsById=new Map<Id, Opportunity>();List<Opportunity> opps= [select id, Contains_Code_96__c from Opportunity WHERE id in :oppIds];oppsById.putAll(opps);
List<Opportunity> oppsToUpdate=new List<Opportunity>();for (OpportunityLineItem oli : trigger.new)
   {          Opportunity opp=oppsById.get(oli.opportunityid);       opp.Contains_Code_96__c = true;       oppsToUpdate.add(opp);   }
update oppsToUpdate;}

 

mngmng

In your second for loop, you may want to add a containsKey check before you get the opportunity from the map and carry on. If the opportunity isn't in the map, it will return null and the line right after that is going to throw the null pointer exception.

 

The opportunity may not be in the map because you filter out which opportunities to get back when you query, only getting back opportunities whose OLIs have that service code set. So perhaps it would make more sense to loop through the opportunities that you got back in the query instead of looping through the OLIs and getting the opptys from the map.

 

Also, make sure to wrap that DML in a try/catch!

Rahul S.ax961Rahul S.ax961

Hi john8407,

 

It seems you have used map and list which are not needed.

I am writing the code for you.

Hope it helps and let me know if you face any issues. :)

 

 

trigger UpdateOpportunityCode96Box on OpportunityLineItem (after update)
{
    List<Opportunity> oppsToUpdate=new List<Opportunity>();
    for(Opportunity objOpp : [select id, Contains_Code_96__c from Opportunity WHERE Id IN (select OpportunityId from OpportunityLineItem where id in: Trigger.new and Magnys_Service_Code_Copy__c = '96')])
    {
        objOpp.Contains_Code_96__c == true;
        oppsToUpdate.add(objOpp);
    }
    if(!oppsToUpdate.isEmpty())
        update oppsToUpdate;

}