I am getting the following error:

System.NullPointerException: Attempt to de-reference a null object Stack Trace: Class.unsubscribe_user.unsubscribeMe: line 21, column 1 Class.unsubscribe_test.unsubscribe_test: line 29, column 1

Could someone tell me what I'm doing wrong?

Apex Test:

private class unsubscribe_test {

    static testMethod void unsubscribe_test() {

        Account acc = new Account();
        acc.Name =  'TestAccount';
        acc.Airport__c = 'LHR';
        acc.Industry = 'Airline';
        acc.LHR__c = true;
        acc.AcctExtID__c = '123';
        acc.Customer_Account_number__c = '112233';
        insert acc;

        Contact co = new Contact();
        co.AccountId = acc.Id;
        co.Email = 'johndoe_test@mail.com';
        co.LastName = 'CONTACT';
        co.Airport__c = 'LHR';
        insert co;

        PageReference pg = Page.Unsubscribe;

        unsubscribe_user controller = new unsubscribe_user();
        // Act
        controller.email = co.Email;

        // Assert
        Contact insertedData = [SELECT Id, Email, HasOptedOutOfEmail FROM Contact WHERE Email = :co.Email];
        System.assertEquals(insertedData.hasOptedOutOfEmail, true);



Apex Controller:

public class unsubscribe_user {
    public String email {
    public String token {

    public unsubscribe_user() {}

    public PageReference unsubscribeMe() {
        try {
            List<Contact> contactList = [
                select Name,
                from Contact
                where Email = :email and
                    Id = :token.substring(10) and
                    hasOptedOutOfEmail = false
                limit 100

            if (contactList.isEmpty()) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Warning, 'You are not in our mailing list (you may have already been removed)'));
                return null;

            for (Contact contact: contactList) {
                contact.hasOptedOutOfEmail = true;

            update contactList;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'You have been successfully removed from our mailing list'));
        } catch (System.QueryException e) {
            System.debug(System.LoggingLevel.ERROR, 'Contact Query Issue: ' + e.getMessage());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Fatal, 'FATAL Error: Please contact the administrator'));

        return null;
  • 1
    I can't tell you what you are doing wrong without seeing the code from unsubscribe_user.unsubscribeMe but the error message is saying that line 21 of the unsubscribe_user class has a NullPointerException. The second line number (29) in the error is referring to the line in your test that called the method the error happened in. – BarCotter Jan 26 '15 at 14:17
  • My appologies, I forgot to add the controller. I have updated the post. – Deployment Failure Jan 26 '15 at 14:18
  • @MihaiNeagoe Ah of course! Please convert into the answer. – Deployment Failure Jan 26 '15 at 14:21
  • Converted into an answer – Mihai Neagoe Jan 26 '15 at 14:27

You are not setting any value for the property token after you instantiate your controller

unsubscribe_user controller = new unsubscribe_user();
// Act
controller.email = co.Email;
controller.token = 'Your value here';

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.