You need to sign in to do that
Don't have an account?

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;}
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!
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. :)