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
piyush parmarpiyush 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

Best Answer chosen by Admin (Salesforce Developers) 
piyush parmarpiyush parmar

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

minkeshminkesh

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.

 

for(OpportunityLineItem  ol:trigger.new) {
        lineId.add(ol.Id);
        oppIdAndLineItems.put(ol.PricebookEntry.Product2Id, ol);
        
   }
piyush parmarpiyush parmar

I have crete productId map not  OpportunitylineItem Id

 

nbknbk

Hi,

 

Add following code..

If(oppIdAndLineItems.ContainsKey(oli.PricebookEntry.Product2Id))
{

       OpportunityLineItem newlyAdded  = oppIdAndLineItems.get(oli.PricebookEntry.Product2Id);
        System.debug('newlyAdded.Quantity-->'+newlyAdded.Quantity);
}

 

 

piyush parmarpiyush parmar

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);
    }

 

 

 

This was selected as the best answer