Error Message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TargetX_Eventsb.preInsertOrgEvent: execution of BeforeInsert

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

(TargetX_Eventsb) : [] Stack Trace Class.StudentAppointmentControllerTest.studentAppointmentTestCase: line 52, column 1

private class StudentAppointmentControllerTest{

    public static testmethod void studentAppointmentTestCase(){

    // Create common test Accounts
    List<Account> testAccounts = new List<Account>();
    Account acc = new Account(Name ='TestAccount'); 
    testAccounts .add(acc);
    insert testAccounts;

    // Create common test Contacts
    List<Contact> testContacts = new List<Contact>();
    contact c = new contact(lastname ='TestContact'); 
    insert testContacts ;

    List<TargetX_Eventsb__OrgEvent__c> orgEventList = new list<TargetX_Eventsb__OrgEvent__c>();
    TargetX_Eventsb__OrgEvent__c orgEvent1 =new TargetX_Eventsb__OrgEvent__c();
    orgEvent1.name = 'test event';
    orgEvent1.TargetX_Eventsb__Start_Date_Time__c = system.now();
    orgEvent1.TargetX_Eventsb__End_Date_Time__c = system.now() + 2;
    orgEvent1.TargetX_Eventsb__type__c = 'Alumni Reception';
    orgEvent1.TargetX_Eventsb__Color__c = '1C62CF';
    orgEvent1.TargetX_Eventsb__unlimited_attendees__c = true;
    orgEvent1.TargetX_Eventsb__Email_Sender__c = 'Use Default';
    orgEvent1.TargetX_Eventsb__Time_Zone__c = 'America/New_York';
    orgEvent1.TargetX_Eventsb__Event_Cost__c = 1.11;
    orgEvent1.TargetX_Eventsb__max_attendees__c = 30;
    orgEvent1.TargetX_Eventsb__Map_URL_Text__c = 'click here for directions';
    orgEvent1.TargetX_Eventsb__scan_before__c = 1.0;
    orgEvent1.TargetX_Eventsb__scan_after__c = 1.0;
    insert orgEvent1;

    TargetX_Eventsb__OrgEvent__c orgEvent2 =new TargetX_Eventsb__OrgEvent__c();
    orgEvent2.name = 'test event2';
     orgEvent1.TargetX_Eventsb__Start_Date_Time__c = system.now()-1;
    orgEvent1.TargetX_Eventsb__End_Date_Time__c = system.now() + 2;
    orgEvent2.TargetX_Eventsb__Color__c = '1C62CF';
    orgEvent2.TargetX_Eventsb__unlimited_attendees__c = true;
    orgEvent2.TargetX_Eventsb__Email_Sender__c = 'Use Default';
    orgEvent2.TargetX_Eventsb__Time_Zone__c = 'America/New_York';
    orgEvent2.TargetX_Eventsb__Event_Cost__c = 1.11;
    orgEvent2.TargetX_Eventsb__Event_Cost__c = 1.11;
    orgEvent2.TargetX_Eventsb__max_attendees__c = 30;
    orgEvent2.TargetX_Eventsb__Map_URL_Text__c = 'click here for directions';
    orgEvent2.TargetX_Eventsb__scan_before__c = 1.0;
    orgEvent2.TargetX_Eventsb__scan_after__c = 1.0;
    insert orgEvent2;

    insert  orgEventList;

    list<TargetX_Eventsb__ContactScheduleItem__c>  csiList = new list<TargetX_Eventsb__ContactScheduleItem__c>();

    TargetX_Eventsb__ContactScheduleItem__c csi1 = new TargetX_Eventsb__ContactScheduleItem__c();
    csi1.TargetX_Eventsb__OrgEvent__c = orgEvent1.id;
    csi1.TargetX_Eventsb__Contact__c = c.id;
    csi1.TargetX_Eventsb__Confirmed__c = true;
    csi1.Type__c = 'Student';
    insert csi1;

    TargetX_Eventsb__ContactScheduleItem__c csi2 = new TargetX_Eventsb__ContactScheduleItem__c();
    csi2.TargetX_Eventsb__OrgEvent__c = orgEvent1.id;
    csi2.TargetX_Eventsb__Contact__c = c.id;
    csi2.TargetX_Eventsb__Confirmed__c = true;
    csi2.Type__c = 'Professor';
    insert csi2;
    insert csiList; 

    StudentAppointmentController studentAppctrl = new StudentAppointmentController();
    studentAppctrl.eventOrgId = csi1.id;


  • This error "System.NullPointerException: Attempt to de-reference a null object" means that you have failed to instantiate something, perhaps a variable, field, or sObject, and you are trying to reference it in code. Look in the Trigger that handles the "TargetX_Eventsb__OrgEvent__c" to see if you are trying to use something that hasn't been instantiated.
    – dBeltowski
    Commented May 27, 2015 at 20:12
  • Do you have a before insert trigger TargetX_Eventsb.preInsertOrgEvent? That's where the problem is
    – navD87
    Commented May 27, 2015 at 20:13
  • 1
    Actually, this may be the result of Copy/Pasted code. After you've created "orgEvent2" you aren't setting the Start_Date_Time or End_Date_Time. You are actually setting it for "orgEvent1".
    – dBeltowski
    Commented May 27, 2015 at 20:16

1 Answer 1


Couple of quick observations

1. You have this code

insert orgEvent1;

orgEvent1 is now added in Database. You are adding this OrgEvent1 in List twice.

2. You are adding to the list another object

insert orgEvent2;

orgEvent2 is added in Database.

3. Now you are again adding this list in database

insert  orgEventList;

Since you have already added its individual objects, what is the need for inserting again. This will result in error.

I am not sure this is causing the problem you reported, please share the complete code with line numbers

  • @Adrian Larson - nice editing! Commented May 27, 2015 at 21:48
  • @CaspNZ Thanks! So close to the Strunk & White badge lol.
    – Adrian Larson
    Commented May 27, 2015 at 21:50
  • WooHoo - only 6 to go! You could do that today! Commented May 27, 2015 at 21:57

You must log in to answer this question.

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