1

Below is my test class in salesforce apex:-

@isTest
public class Testopoffers{
    @isTest
    private static void testRun(){
        Contact c = new Contact(AccountId = '001g000000OPOr1',LastName = 'test');
        insert c;
        Profile p = [SELECT Id FROM Profile WHERE Name='OnlineForms User']; 
        User u = new User(Alias = 'standt', Email='[email protected]', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',ContactId = c.id, 
        LocaleSidKey='en_US', ProfileId = p.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');
        System.runAs(u){
        System.debug('Current User: ' + UserInfo.getUserName());
        System.debug('Current User ID: ' + UserInfo.getUserId());
            dsmtOpOffersCntlr dopc = new dsmtOpOffersCntlr();
            String pageRef = dopc.updateDSMTracker().getUrl();
            System.assertEquals('/default/dsmtManageEnrollmentApplicationUser', pageRef);
        }
    }

}

And below is the class and function for which the above test class is for :-

global class dsmtOpOffersCntlr{

    public pagereference updateDSMTracker(){
       u.DSMTracker_Show_Help_Splash_Screen__c=true;
       update u;
       return null;
    }

    public dsmtOpOffersCntlr(){

        u =[Select DSMTracker_Show_Help_Splash_Screen__c  from User where id=:UserInfo.getUserId()];       

    }
}

The above test class is giving the following error in debug logs :- System.NullPointerException: Attempt to de-reference a null object

User is creating correctly for your information. Please can anybody give me correct syntax to write the test class for above class and method ? Thanks in advance.

5
  • What line is throwing the NullPointerException error?
    – BarCotter
    Jan 21, 2015 at 13:53
  • Have a look at an answer I wrote a few months ago, it might be helpful salesforce.stackexchange.com/questions/60329/… Jan 21, 2015 at 13:56
  • 1
    You have to insert your user. You create the User u, but do not insert it Jan 21, 2015 at 13:56
  • String pageRef = dopc.updateDSMTracker().getUrl(); after this line the error is coming. I have inserted the user but still same error Jan 21, 2015 at 14:11
  • It doesn't appear that the user record you've inserted has a DSMTracker_Show_Help_Splash_Screen__c value at the time of the select, so you're getting a Null return? I could be wrong. Why not set the value in the user insert during the test?
    – HomerJ
    Jan 21, 2015 at 15:34

1 Answer 1

2

The method below returns null in your code

public pagereference updateDSMTracker(){
   u.DSMTracker_Show_Help_Splash_Screen__c=true;
   update u;
   return null;
}

When the line below executes you are getting a NullPointerException because you are trying to call getUrl() on null

String pageRef = dopc.updateDSMTracker().getUrl();
1
  • 1
    I believe I just said this earlier, but that's exactly why you shouldn't use a PageReference return type if you don't intend to return a value. It avoids mistakes like this.
    – sfdcfox
    Jan 21, 2015 at 18:18

You must log in to answer this question.

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