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
Prasanth Reddy MPrasanth Reddy M 

test class compilation error

Hi, I am getting nullpointer exception while running below test class.

After inserting both contact and campaign objects only, i am referring them in campaign member object, wondering what i am missing here.
@isTest

public class CampaignMemberTaskcreation {

    Static testmethod void testtask(){

        Contact co1 = new Contact(
        LastName = 'Last',
        Title='CEO');
        
        insert  co1;
        string co1Id = co1.id;

        Campaign ca1 = new Campaign(
            Name = 'Testcampaign',
            Date_Sent__c=Date.today(),
            IsActive = TRUE);
        insert ( ca1 );
        string ca1Id = ca1.id;

        CampaignMember m1 = new CampaignMember();
            m1.Contact.Id = co1.Id; 
            m1.Campaign.Id = ca1.Id;
            m1.status='Responded';
        insert m1;
    }

}



Below is the error
Class CampaignMemberTaskcreation
Method Name testtask
Pass/Fail Fail
Error Message System.NullPointerException: Attempt to de-reference a null object
Stack Trace Class.CampaignMemberTaskcreation.testtask: line 22, column 1
Best Answer chosen by Prasanth Reddy M
Marek Kosar_Marek Kosar_
Hello,

creation of campaignMember:
CampaignMember m1 = new CampaignMember();
    m1.ContactId = co1.Id; 
    m1.CampaignId = ca1.Id;
    m1.status='Responded';
insert m1;