You need to sign in to do that
Don't have an account?
piyush parmar
System.NullPointerException: Attempt to de-reference a null object
Hi All,
I created trigger(after insert) on OpportunityLineItem to mergr same product .
But a\ i am getting System.NullPointerException: Attempt to de-reference a null object: .
Map<Id,OpportunityLineItem> oppIdAndLineItems = new Map<Id,OpportunityLineItem>();
for(OpportunityLineItem ol:trigger.new) {
lineId.add(ol.Id);
oppIdAndLineItems.put(ol.PricebookEntry.Product2Id, ol);
}
for( OpportunityLineItem oli : [select Id,PricebookEntry.Product2Id,Quantity,ListPrice,TotalPrice from OpportunityLineItem where Id in: lineId] ) {
productIds.add(oli.PricebookEntry.Product2Id);
}
List<OpportunityLineItem> toBeDeleted = new List<OpportunityLineItem>();
List < OpportunityLineItem > toBeUpdated = new List < OpportunityLineItem >();
for( OpportunityLineItem oli : [select Id,PricebookEntry.Product2Id,Quantity,ListPrice,TotalPrice from OpportunityLineItem where PricebookEntry.Product2Id in: productIds and Id not in: lineId] ) {
OpportunityLineItem newlyAdded = oppIdAndLineItems.get(oli.PricebookEntry.Product2Id);
System.debug('newlyAdded.Quantity-->'+newlyAdded.Quantity);
newlyAdded.Quantity = newlyAdded.Quantity + oli.Quantity;
toBeDeleted.add(oli);
toBeUpdated.add(newlyAdded);
}
i have highlight line when i am getting this error .
Plz give me some idea what i am doing wrong !!
Thank You,
piyush
Solved !!!
for(OpportunityLineItem ol:trigger.new) {
lineId.add(ol.Id);
// productIds.add(ol.PricebookEntry.Product2Id);
// oppIdAndLineItems.put(oli.PricebookEntry.Product2Id, oli);
}
for( OpportunityLineItem oli : [select Id,PricebookEntry.Product2Id,Quantity,ListPrice,TotalPrice from OpportunityLineItem where Id in: lineId] ) {
productIds.add(oli.PricebookEntry.Product2Id);
oppIdAndLineItems.put(oli.PricebookEntry.Product2Id, oli);
}
All Answers
Hello Piyush,
Look at your first for loop in that u are creating lineId and oppIdAndLineItems.
now look at your next for loop in that u are getting record from map but in that for loop your query is wrong because u are fetching those data which is not in that line id so u are getting null exception.
I have crete productId map not OpportunitylineItem Id
Hi,
Add following code..
If(oppIdAndLineItems.ContainsKey(oli.PricebookEntry.Product2Id))
{
OpportunityLineItem newlyAdded = oppIdAndLineItems.get(oli.PricebookEntry.Product2Id);
System.debug('newlyAdded.Quantity-->'+newlyAdded.Quantity);
}
Solved !!!
for(OpportunityLineItem ol:trigger.new) {
lineId.add(ol.Id);
// productIds.add(ol.PricebookEntry.Product2Id);
// oppIdAndLineItems.put(oli.PricebookEntry.Product2Id, oli);
}
for( OpportunityLineItem oli : [select Id,PricebookEntry.Product2Id,Quantity,ListPrice,TotalPrice from OpportunityLineItem where Id in: lineId] ) {
productIds.add(oli.PricebookEntry.Product2Id);
oppIdAndLineItems.put(oli.PricebookEntry.Product2Id, oli);
}