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
Rebecca YorkRebecca York 

Help with Error:System.NullPointerException: Attempt to de-reference a null object

Hi,

I am pretty new to SalesForce coding and hoping you can help.

I am getting the following error: Error:Apex trigger CreateCompetitor caused an unexpected exception, contact your administrator: CreateCompetitor: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CreateCompetitor: line 5, column 1. 

My research seems to be telling me that it is throwing an error when the field I am referencing is blank. Sometimes this field will need to be blank and I don't want to run the trigger in that case, can someone please help?

Below is my code (which will eventually need to be built out some more to include more competitors)

trigger CreateCompetitor on Opportunity (after insert, after update) {
 for(Opportunity o: trigger.new){ 
    String opptyId = o.Id;
    Opportunity oldOpp = Trigger.oldMap.get(o.Id);
    if(o.Competitors__c.contains('Competitor 1') && !oldOpp.Competitors__c.contains('Competitor 1')){
    Competitor__c[] OLI = [Select Id, Name  
                           From Competitor__c
                           where Name = 'Competitor 1'];
      Link_Competitor_with_an_Opportunity__c[] ast = new Link_Competitor_with_an_Opportunity__c[]{};
         Link_Competitor_with_an_Opportunity__c m = new Link_Competitor_with_an_Opportunity__c();
                  for(Competitor__c ol: OLI){
            m = new Link_Competitor_with_an_Opportunity__c();    
            m.Opportunity__c = o.Id;
            m.Competitor__c = ol.Id;
            ast.add(m);
            update OLI;
            insert ast;
    if(o.Competitors__c.contains('Competitor 2') && !oldOpp.Competitors__c.contains('Competitor 2')){
    Competitor__c[] OLI2 = [Select Id, Name  
                           From Competitor__c
                           where Name = 'Competitor 2'];
      Link_Competitor_with_an_Opportunity__c[] ast2 = new Link_Competitor_with_an_Opportunity__c[]{};
         Link_Competitor_with_an_Opportunity__c m2 = new Link_Competitor_with_an_Opportunity__c();
                  for(Competitor__c ol2: OLI2){
            m2 = new Link_Competitor_with_an_Opportunity__c();    
            m2.Opportunity__c = o.Id;
            m2.Competitor__c = ol2.Id;
            ast2.add(m2);
            update OLI;
            insert ast2;
            }}}}}}

Thanks in advance!
ManjunathManjunath

Hi Rebecca,

In your code your are using the contains methon on the field which is null. So before using the contains check if it is null
Instead of this
 if(o.Competitors__c.contains('Competitor 1') && !oldOpp.Competitors__c.contains('Competitor 1')){

Try something like this.
 if(o.Competitors__c!=null && o.Competitors__c.contains('Competitor 1') && oldOpp.Competitors__c!=null && !oldOpp.Competitors__c.contains('Competitor 1')){

revisit your if condition.

Regards,
Manjunath C Sarashetti
Rebecca YorkRebecca York
Brilliant, thank you Manjunath!