0
    Account acct = (Account)SmartFactory.createSObject('Account');
    acct.Name = 'Newman Brodsky';
    insert acct;

    Contact contact = (Contact)SmartFactory.createSObject('Contact');
    contact.FirstName = 'New';
    contact.LastName = 'Contact';
    contact.AccountId = acct.Id;
    insert contact;

    Campaign c = (Campaign)SmartFactory.createSObject('Campaign');
    c.Name = 'MC Campaign';
    insert c;

    CampaignMember m = (CampaignMember)SmartFactory.createSObject('CampaignMember');
    m.Contact.Id = contact.Id;
    m.Campaign.Id = c.Id;
    insert m;

I have a basic test class here. Any idea why line m.Contact.Id = contact.Id;

is giving me this null pointer exception?

1
  • Try renaming the contact variable to something other than contact.
    – Nick Cook
    Jul 20 '16 at 3:09
4
m.ContactId = contact.Id;   
m.CampaignId = c.Id;

When you use dot notation, you're referencing an object. By default, standard lookup fields use Id for the actual Id, and without as a reference to the related object. These are null by default, hence the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.