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.

public class CampaignMemberTaskcreation {

    Static testmethod void testtask(){

        Contact co1 = new Contact(
        LastName = 'Last',
        insert  co1;
        string co1Id =;

        Campaign ca1 = new Campaign(
            Name = 'Testcampaign',
            IsActive = TRUE);
        insert ( ca1 );
        string ca1Id =;

        CampaignMember m1 = new CampaignMember();
            m1.Contact.Id = co1.Id; 
            m1.Campaign.Id = ca1.Id;
        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_

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