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
zen_njzen_nj 

trigger code - getting System.NullPointerExcecption: attempt to de-reference a null object

Hi all

I am writing a trigger code on the Case object that saved/compiled fine, but when I test it out by actually creating a new Case entry, I get an error on screen about: Apex trigger <triggerName> caused an unexpected exception.


Specifically, my trigger code only contains the following lines:

trigger CloseoutParentSRCancelCase_allChildCaseClosed on Case (after insert, after update) {
Case[] cc = Trigger.new;

for (Case c:cc)
{
/* only fire the trigger when record type is service request, or when Original Business Unit  */
/* contains 'Cancel Service' keyword and the record type is NOT Customer Trouble Ticket */

       if ((c.RecordTypeId == '0120000000002H7') || ((c.RecordTypeId != '0120000000002G4') && c.OriginalBusinessUnit__c.contains('Cancel Service'))) */
CloseParentSRCancelCase_ChildCaseClosed.CloseoutParentSRCancelCase(c);
}
}


And essentially, I want the trigger to fire off when the Case record type is a specific record type, or it is not a specific record type but the OriginalBusinessUnit__c (custom field) would contain the phrase 'Cancel Service'.

So this works fine when I am creating a new Case entry where RecordType is '0120000000002H7' - meaning it will proceed to call the trigger logic defined in my class method (CloseParentSRCancelCase_ChildCaseClosed.ClosedoutParentSRCancelCase).

And it also works fine when I am creating a new Case entry where RecordType is '0120000000002G4', which I think means it by-pass the class method since none of the condition is met.

However, if I create a new Case entry where RecordType is something else (neither '0120000000002H7' or '0120000000002G4'), and the OriginalBusinessUnit__c is left blank, then I get this trigger exception error.

I would have thought that since the OriginalBusinessUnit__c is a blank value, the condition in blue would not have been met and so it would behave the same way as when I had just created a Case where RecordType is '0120000000002G4' 

The OriginalBusinessUnit__c is a picklist field which would contain various values like:
ABC - Billable Service
ABC - Cancel Service
DDD - Billable Service
DDD - Cancel Service
.
.
ZZZ - Billable Service
ZZZ - Cancel Service

So rather than trying to list out all the entries that has Cancel Service, I am using the contain syntax. So is that the problem?

Can someone please help shed some light on this? thx in advance!

aalbertaalbert
The easiest way to troubleshoot something like this is to put in System.debug() messages in both your Trigger and your Class methods (CloseOutParentSRCancelCase).

For example, add System.Debug('Processing Case with Record Type Id: ' + c.RecordTypeId);
You could also add some debug statements to identify which part of the IF statement was successful.

Also, did the error message state what line had the NullPointerException? If so, that should help narrow down which variable or object is null and you can work backwards from there.

More information on debugging here: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_debugging_debug_log.htm

zen_njzen_nj
thx for the tip on debugging.

The error message did specify where the exception occurred:

Apex trigger CloseoutParentSRCancelCase_allChildCaseClosed caused an unexpected exception, contact your administrator: CloseoutParentSRCancelCase_allChildCaseClosed: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CloseoutParentSRCancelCase_allChildCaseClosed: line 11, column 88

And that line/column 88 is right before the condition && c.OriginalBusinessUnit__c.contains('Cancel Service')

if ((c.RecordTypeId == '0120000000002H7') || ((c.RecordTypeId != '0120000000002G4') && c.OriginalBusinessUnit__c.contains('Cancel Service')


I finally figured out what the problem was. It looks like if the new Case entry I am creating has no value for OriginalBusinessUnit field, then that's why it's failing - I guess the contains utility requires that there is a value in the field. So I revised my logic as followed and it looks to be working fine now:

if (c.RecordTypeId == '0120000000002H7') 
CloseParentSRCancelCase_ChildCaseClosed.CloseoutParentSRCancelCase(c);
else
if ((c.RecordTypeId != '0120000000002G4') && c.OriginalBusinessUnit__c != NULL)
if (c.OriginalBusinessUnit__c.contains('Cancel Service'))
CloseParentSRCancelCase_ChildCaseClosed.CloseoutParentSRCancelCase(c);