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

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
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
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:
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
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 T
www.levementum.com