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
MikeCampMikeCamp 

Before Trigger giving System.NullPointerException: Attempt to de-reference a null object

I have a fix, but anyone have any ideas why this is happening?

Is my solution the best way to address error?

 

I have the following trigger giving me an "System.NullPointerException: Attempt to de-reference a null object" error intermittent error on  line 28 - newAttendee.Contact__c.addError('This contact is already registered for this event.');

 

Using test data in a sandbox the trigger works on some and throw exception on others.

 

Here is an overview of custom objects:

Event

- Registration

- Lookup from Registration to Event

- - Attendee

- - Master detail between Registration and Attendee

- - EventDetail__c is a formula  {eventID-contactID}

 

Here is the trigger

trigger TRG_Attendee_singleContactPerEvent on Attendee__c (before insert, before update) {
    // Make sure we are not adding a Attendee to the same Event     
        
    Map<String, Attendee__c> attendeeMap = new Map<String, Attendee__c>();
    for (Attendee__C Attendee : System.Trigger.new) {
        
        // Only check if adding to an active Registration.  
        if (Attendee.Registration__r.Status__c  != 'Canceled')
            {
            // Make sure another new Attendee in batch isn't also a duplicate  
            if (attendeeMap.containsKey(Attendee.EventContact__c)) {
                // If another new Attendee in batch is a duplicate set error  
                Attendee.Contact__c.addError('This contact is already registered for this event.');
            } else {
                // Add to list for check
                attendeeMap.put(Attendee.EventContact__c, Attendee);
            }
        }
    }
    

    // Using a single database query, find all the Attendees in  
    // the database that have the same EventContact as any  
    // of the Attendees being inserted or updated.  
    for (Attendee__C foundAttendee : [SELECT EventContact__c FROM Attendee__c
                      WHERE EventContact__c IN :attendeeMap.KeySet() and Registration_Status__c = 'Active']) {
            Attendee__C newAttendee = attendeeMap.get(foundAttendee.EventContact__c);
            newAttendee.Contact__c.addError('This contact is already registered for this event.');
    }
}

 

I have fixed by putting if statement around line 27 and 28 like:

        if (attendeeMap.containsKey(foundAttendee.EventContact__c)){
            Attendee__C newAttendee = attendeeMap.get(foundAttendee.EventContact__c);
            newAttendee.Contact__c.addError('This contact is already registered for this event.');
        }

 

dmchengdmcheng

I think your line should be:

Attendee.addError('This contact is already registered for this event.');

The addError applies to the Attendee record, not to any of its fields or related objects.

 

kibitzerkibitzer

Subtle problem.

 

I'm not 100% sure, but I think I have it - and if I'm right, your fix isn't what you're looking for.

 

It's not entirely clear to me what type EventContact__c is, but you are using a String as the map's key, so I assume it's a string.

 

Let's say you have a new registration and EventContact__c is "Joe Smith". The registration succeeds.

He tries registring a second time, but this time enters his name in lower case "joe smith"

 

The attendeeMap map will contain "joe smith" as the key.

 

The query will find the existing entry for "Joe Smith" because the query is case insensitive.

 

But the map key lookup is case sensitive, so "Joe Smith" won't be found in attendeeMap and newAttendee will be  null, leading to a null exception error.

 

One possible solution would be to convert EventContact__c to lower case before adding it to the map and for other comparisons.

 

Anyway, that's my best guess based on the information provided. It does explain what you are seeing.

 

Best of luck


Dan

 

 

 

kibitzerkibitzer

dmcheng:

 

Actually, the addError syntax he is using is unusual, but technically correct. It flags the UI to present the error message near the specified field. SFDC docs note that this is a specialized syntax that is likely to be deprecated in future versions of the language.

 

Dan

 

MikeCampMikeCamp

kibitzer, Thanks for your input.

EventContact__c is string, but it is the Event__c.ID and '_' and the Contact.ID fields concatenated  (a0AK0000003mZ45_003K000000I21Kg), so case sensitivity should not be an issue and  changing case will cause  issues.

MikeCampMikeCamp

dmcheng wrote:

I think your line should be:

Attendee.addError('This contact is already registered for this event.');

The addError applies to the Attendee record, not to any of its fields or related objects.

 


kibitzer is correct, adding the field tells the addError method to target that field in the view.  I will try it without, but I believe the SOQL is returning something that is not in my map.

 

Thanks

kibitzerkibitzer

Right - in that case it's probably not case sensitivity. More likely it's a conflict between 15 and 18 character IDs.  15 character case sensitive IDs are used in the UI, 18 character case insensitive IDs are used in the API.

 

Still - it's hard to see how it could be anything other than a case sensitivity issue - I find it very hard to believe that SOQL will pull a value that isn't in the map.


Dan

 

Gunners_23Gunners_23

Hi,

 

I guess kibitzer is rite. If we're query only for id then eventhough searching for 18 digit id by passing 15 digit id will fetch the

 

values. But in this case its 15digit id appended by __ and again 15 digit Id thats the reason its not fetching the value.

 

MikeCamp,

 

Would you pls care to tell us how the value is getting generated in that field ( Formula field)??