+ Start a Discussion
tmbarrytmbarry 

System.NullPointerException: Attempt to de-reference a null object - Not sure Why this Happening.

I wrote a trigger to create a task when a certain field is updated.  The trigger works fine and as expected.  However, everytime I run the test class I get the following error:

 

System.DmlException: Update failed. First exception on row 0 with id a0NW0000000ddmlMAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Create_Make_1st_Call: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.Create_Make_1st_Call: line 43, column 1: []
Class.TestClass_SD_Member_Mailing_History.myUnitTest: line 39, column 1

 

Here is my Test Class:

@isTest
class TestClass_SD_Member_Mailing_History {    
static testMethod void myUnitTest() {        
// TO DO: implement unit test       

// Create SD Member Record
SD_Member__c sd = New SD_Member__c(First_Name__c = 'John', 
                                   Last_Name__c = 'Smith', 
                                   Ins_ID_Member__c = '123456', 
                                   Ins_ID_Suscriber__c = '456789',
                                   address__c = '123 Main Stret', 
                                   city__c = 'Aster', 
                                   state__c = 'CT', 
                                   zip__c = '06001', 
                                   name = 'John Smith',
                                   Lost_Contact_Follow_Up__c = False,
                                   Mailing_Cycle__c='Week 2.1');
insert sd;
          
// Create Mailing Order
printsf__Collateral_Order_History__c PO = new printsf__Collateral_Order_History__c(Name='Dummy Order 02',
                                                                                   printsf__Order_Number__c=2);
Insert po;                                                                                              

// Create SD Mailing History Item
printsf__Collateral_Send_History__c MH = new printsf__Collateral_Send_History__c(Name='Test Mailer Entry XYZ',
                                                                                 printsf__Recipient_ID__c = sd.id,
                                                                                 printsf__Send_History_ID__c = 123654,
                                                                                 printsf__Order__c = po.id);
Insert MH;

//Test Order Execute logic for send "make first Call" task
SD_Member_Mailing_History__c SDMH = New SD_Member_Mailing_History__c(Mailer_History_Item__c =MH.id ,
                                                                     SD_Member_Stakeholders__c = sd.id);
Insert SDMH;

SDMH.Order_Executed__c = True;
system.debug(sd.id);
Update SDMH;


}}

 

Here is my trigger.

trigger Create_Make_1st_Call on SD_Member_Mailing_History__c (After Update) {
//This is the real one!!!!!!

// Variables
    Date CallDate1 = DateTime.now().addDays(-10000).date();

// Create my own Right Text Function
public String right(String s, Integer i) {
        if (s == null || s == '' || i <=0 ) {
            return '';
        } else if (i >= s.length()) {
            return s;
        } else {
            return s.subString(s.length() - i, s.length());
        }
} 
//-----------------------------------------------------------------------

 // Find Entries that meet criteria and new Entries to create
SD_Member_Mailing_History__c [] qualifiedEntry = new SD_Member_Mailing_History__c[0];
Task [] newTask = new Task[0];

// Map of SD Member Ids
    Map<Id,SD_Member__c> members = new Map<Id,SD_Member__c>();
    
// Find qualifing Entries
For (SD_Member_Mailing_History__c record:trigger.new)  
    If(record.Order_executed__c ==True)
           qualifiedEntry.add(record);  

//Obtain SD Member Ids 
For(SD_Member_Mailing_History__c record:qualifiedEntry)   
    members.put(record.SD_Member_Id__c,null); 
    
// If there are any members to query, do so.
   If(!members.isempty())
       members.putall([Select id, Mailing_Cycle__c, Ownerid from SD_Member__c where id in :members.keyset()]);       

// For each qualifing record, create a task.
For(SD_Member_Mailing_History__c record:qualifiedEntry){
// Check to see what mailing cycle the SD Member is in to determine when to make the follow up call         
         If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='1') {
           newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+3,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         } Else If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='2') {
             newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+4,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         } Else If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='3') {
             newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+5,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         } Else If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='4') {
             newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+6,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         } Else If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='5') {
             newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+7,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         } Else If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='6') {
             newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+8,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         } Else If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='7') {
             newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+9,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         } Else If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='8') {
             newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+10,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         } Else If(right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)=='9') {
             newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+11,Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         //Old Activity Date Logic
         //ActivityDate=DateTime.now().addDays(11).date()
         } 
}
   // Insert new task records into database.
      insert newtask;
}

 

 

Everything was working fine until I changed one line of code in my trigger.  

 

I change the if statements from 

 

newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=DateTime.now().addDays(11).date(),Status='Not Started', ownerid=members.get(record.SD_Member_id__c).ownerid));
         

 To

 

newtask.add(new task(whatid=(record.SD_Member_id__c),subject='Make 1st Follow Up Call',ActivityDate=record.Date_Delivered__c+11,Status='Not Started'

Any Ideas

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

When you create the record to update

 

SD_Member_Mailing_History__c SDMH = New SD_Member_Mailing_History__c(Mailer_History_Item__c =MH.id ,
                                                                     SD_Member_Stakeholders__c = sd.id);

 

You are not populating the Mailing_Cycle__c

 

and when you call

right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)

The value is null thus the right() is throwing the null pointer

 

All Answers

Starz26Starz26

When you create the record to update

 

SD_Member_Mailing_History__c SDMH = New SD_Member_Mailing_History__c(Mailer_History_Item__c =MH.id ,
                                                                     SD_Member_Stakeholders__c = sd.id);

 

You are not populating the Mailing_Cycle__c

 

and when you call

right(members.get(record.SD_Member_id__c).Mailing_Cycle__c,1)

The value is null thus the right() is throwing the null pointer

 

This was selected as the best answer
tmbarrytmbarry

Thanks Starz26