I have trigger that on deployment is throwing a NullPointerException. The error is:
Trigger.tgr_send_enrollment: line 13, column 1: [] System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, tgr_send_enrollment: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object
I have a test class that creates a contact "con". I have a trigger that does some stuff after insert and after update. The deployment process is failing because of a nullpointer exception on the after insert part of the trigger. The test class inserts a value for con.CurrentProcessor__c, however when the trigger is tested, there's no value for con.CurrentProcessor__c so we get a null pointer exception
What I don't understand is why the value of the field con.CurrentProcessor__c is showing up as null (the null pointer exception) when I've specifically inserted it as part of the test class.
here is the test class:
@istest
/*This test class should test three different pieces of code:
1. cls_Enrollment_Service
2. cls_send_enrollment
3. tgr_send_enrollment
*/
public class test_cls_send_enrollment
{
public static testmethod void test_cls_send_enrollment1()
{
contact con = new contact();
con.lastname = 'test';
con.Status__c='Enrolled';
con.ProcessingStatus__c='Confirmed';
con.CurrentProcessor__c='PROC-SUBA';
con.UniqueID__c = '23332';
con.DebitAccountType__c ='Checking';
con.LoanNameType__c = 'Bill1';
con.PaymentType__c = 'Electronic';
system.debug('*******CurrentProcessor in test is: '+con.CurrentProcessor__c);
insert con;
cls_send_enrollment c = new cls_send_enrollment();
cls_send_enrollment.cls_send_enrollment_XML(con.id);
//test #3 tgr_end_enrollment below
con.ProcessingStatus__c='Proc Error';
update con;
con.ProcessingStatus__c='Confirmed';
}
}
And here is the trigger; note that I've put in a couple of debugging variables in here. I wouldn't necessarily finalize the code like this.
trigger tgr_send_enrollment on Contact (after insert,after update)
{
String processor = '';
String LeftProcessor ='';
for(contact con:trigger.new)
{
//need to grab the left 4 character of the current processor and test
processor=String.valueof(con.CurrentProcessor__c);
system.debug('*******Processor is: '+processor);
LeftProcessor=processor.substring(0,4); // THIS IS LINE 13
system.debug('*******Processor Substring is: '+LeftProcessor);
system.debug('*******CurrentProcessor is: '+con.CurrentProcessor__c.substring(0,4));
if((con.CurrentProcessor__c).substring(0,4)=='PROC') { //Should only enroll if the processor selected is PROC
system.debug('*******InsideIF test is: '+con.CurrentProcessor__c.substring(0,4));
if(trigger.isinsert)
{
if((con.Status__c=='Enrolled')&&(con.ProcessingStatus__c=='Confirmed')
{
{do some stuff};
}
}
if(trigger.isupdate)
{
{test some stuff}
{
{do some stuff}
}
}
}
}
}
In terms of the debugging statements, when the trigger is running after insert the system.debug('*******Processor is: '+processor);
yields a *******Processor is: null.
And when the trigger is in after update mode (second part of the test class), it yields yields a *******Processor is: PROC.
(the update works, the insert DOES NOT!).
So what did I do incorrectly here?