0

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?

3 Answers 3

1

Be sure to check if you have any workflow, or process boiler, of before insert trigger that could empty the field. BTW, what is the value of con.CurrentProcessor__c at the very beginning of a before insert trigger ?

1
  • Thanks, this was a good suggestions. I changed the trigger to be a before insert trigger to see what would happen, same problem. Though it helped me lead to the solution... Commented Jun 10, 2015 at 16:28
1

Check your org's required fields for a Contact. I strongly suspect that you'll discover you don't have all of the required fields and that's why your contact isn't being inserted. I don't see any address or phone numbers fields, email, etc being included in your contact creation code.

Is there a record type needed for this type of contact? That could also be an issue of concern as well. Your debug logs if set at a high enough level should show you this information. Please see How do I start to debug my own Apex code?.

1
  • Thanks for the suggestions. All the required fields were filled in, though, on a separate note, interestingly I don't seem to ever create Accounts for corresponding Contacts in my tests for Contacts (I noticed that creating an account is required when manually creating a contact)... hasn't been an issue yet, so I'm not complaining. Commented Jun 10, 2015 at 16:28
1

Found the answer. I had separate test classes that were also creating contacts without the field I was referencing. And those test classes were running first! If anyone knows how to control the order test classes run, please let me know. Probably, the right answer is to consolidate all those test classes into one...

So, I had to go into those test classes and also make sure con.CurrentProcessor__c='PROC-SUBA'; line was in all those test classes too. Obviously the easy solution is to insert some defensive coding, but I wanted to make sure I understood why the error was happening.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .