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
Jeff GJeff G 

Opportunity (assoc. w/ an Oppty Product) is null -- dereferencing a null object error

Hi all,

I am writing a trigger which should aggregate the info on all Opportunity Products associated with an Opportunity, and then populate this info onto the Opportunity in the form of checking the relevant checkboxes. Ex: if product A is based in Houston and product B is based in New York, then the checkboxes for both "Houston" and "New York" should be checked on the Opportunity associated with products A and B.

The problem I'm having is that the Opportunity I am referencing through my Opportunity Product appears to be a null object. When I try to set a checkbox value to true, for example, I get the following error:
TestUpdateOppCheckboxes: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object

Yes, I can check for a null value or surround my code with a try/catch block, and this would avoid the exception, but that is not the point. I would like to know why the Opportunity I am looking up through my Opportunity Product is a null object. Here is a part of my trigger so far:

trigger TestUpdateOppCheckboxes on OpportunityLineItem (before insert, before update) {
     for(OpportunityLineItem OppItemRecord : Trigger.new) {
           Opportunity o = OppItemRecord.Opportunity;

           if(OppItemRecord.Location__c.contains('NY') && !OppItemRecord.Opportunity.NY__c) {
                 o.NY__c = true;     //here the trigger should be checking off the 'NY' checkbox on the opportunity
                                                 //this is where I would get the exception, since I would be trying to derefence 'o', which is null
           }
     }
}

Any suggestions would be great, thanks in advance!

Best,
Jeff
Cyrus TalladenCyrus Talladen
As far as I can see by reading your partial code, it could be that you should make use of a map that contains the old values such as 
Map<Id,Opportunity> oldMap

since the operation you are getting error from is the before update.  Also, it would be more maintainable for you in the future if you use separation of concerns by using a trigger handler class and separating the trigger events for example:
if(trigger.isBefore){
  if(trigger.isUpdate){
    myTrgHandler.staticMethod(trigger.oldMap);
 }
}

so that later you can insert your debug statements in the proper line which allows you to locate where the null pointer exception is coming from

Cyrus T.
www.levementum.com
Jeff GJeff G
Cyrus,

Thanks for the reply. I am trying to use a map as you suggested, but I am again getting the same error here (italicized):

Map<Id, Opportunity> oldMap;
    for(OpportunityLineItem OppItemRecord : Trigger.new) {
        oldMap.put(OppItemRecord.Id, Trigger.oldMap.get(OppItemRecord.Id).Opportunity);
  }

Any idea why this may be?

-Jeff
Cyrus TalladenCyrus Talladen
If we are sure the  error is in that block of code then OppItemRecord could be null since it was initialzed without a query.  We get a null reference error because we are trying to use an object that was initialized with null values

Cyrus T
www.levementum.com