0

Here is my trigger, class, and test code. Why is my test code not passing?

System.NullPointerException: Argument cannot be null. Class.LocationUpdateReferral.updateCreatedLocationsonReferral: line 13, column 1

Trigger

trigger LocationTrigger on Location__c (after update) {
    for (Location__c loc : trigger.new){
        if ( (trigger.oldMap.get(loc.Id).status__c != 'Lost' && loc.Status__c == 'Lost') || (trigger.oldMap.get(loc.Id).status__c != 'Denied' && loc.Status__c == 'Denied')){
            id refId;
            string facility;
            if (loc.Referral__c != null && loc.Location__c != null){
                refId = loc.Referral__c;
                facility = loc.Location__r.name;
                Set<string> locationNameSet = new Set<string>();
                locationNameSet.add(facility);
            LocationUpdateReferral.updateCreatedLocationsonReferral(refId, locationNameSet );
        }    
        }
}
}

Class

public class LocationUpdateReferral {
@future (callout = true)
    public static void updateCreatedLocationsonReferral(id refId, set<string> facility){
        
        lead ref = [select id, All_Locations_Hidden__c from lead where id = : refId];
        
        if (ref.All_Locations_Hidden__c != null){
            
            for (string loc : facility){
                string semiFirst = ';' + loc;
                string semiLast = loc + ';';
                
                if (ref.All_Locations_Hidden__c.contains(loc)){
                    ref.All_Locations_Hidden__c =  ref.All_Locations_Hidden__c.remove(loc);
                }
                else if (ref.All_Locations_Hidden__c.contains(loc)){
                    ref.All_Locations_Hidden__c =  ref.All_Locations_Hidden__c.remove(loc);
                }
            }
            update ref;
        }
        return;
    }
}

Test

@isTest
public class LocationCLeadHandlerTest {
    
    static testMethod void refTestMethod(){
        Id facilityAccountid = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Facility Account').getRecordTypeId();
        Account a = new Account(name = 'Essex Center', recordtypeid = facilityAccountid);
        insert a;

        date d = date.today();
        Lead l = new Lead(firstname = 'John', lastname = 'Smith', patient_dob__c = d, insurancecompany__c = acc.id, All_Locations_Hidden__c = 'Essex Center;');
        insert l;
        
        Location__c loc = new Location__c (referral__c = l.id, location__c = a.Id);
        insert loc;
        
        loc = [select id, status__c, denial_lost_reason__c from location__c where id = :loc.id];
        loc.Status__c = 'Lost';
        loc.Denial_Lost_Reason__c = 'Other SNF';
        update loc;
    }
}
2
            facility = loc.Location__r.name;
            Set<string> locationNameSet = new Set<string>();
            locationNameSet.add(facility);
            

Triggers don't have access to relational data. loc.Location__r.Name is null. You have to query for relationship data you want to access in a trigger.

NullPointerException is one of the rare instances where the error really doesn't lie or deceive you. It always means that something is null. That means that resolving it is simply a matter of following the null value back from the error to its source.

2
  • 1
    Additionally, we can know that this is the issue because your error was argument cannot be null. Meaning loc is null on line 13 (ref.All_Locations_Hidden__c.contains(loc)). Debugging NPEs is usually an exercise in deduction (determining what can possibly be null, and then working backwards to find out where the null came from). – Derek F Nov 18 '20 at 14:31
  • Thank you for that information this helped solve the issue – Michael M Nov 18 '20 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.