I am getting null pointer excaption in Test class in below Lines Class.CasepopulateDefault.Redirect: line 14, column 1 Class.CasepopulateDefaultTest.defaultcase: line 22, column 1

PLs let me know what is missing

public class CasepopulateDefaultTest {

static testmethod void defaultcase(){

    List<case> caseListForSC = TestDataFactory.createCase_SelfService(1,'General');
    ApexPages.StandardController sc = new ApexPages.StandardController(caseListForSC[0]);

    string recordtypeId = '';
    recordtype recordtypeInst = [select Id from recordtype where developername = 'Technical_Support' and sobjecttype = 'Case'];
    recordtypeId = recordtypeInst.Id;

    // Set up User
    String ProfileId = [select Id from profile where Name = 'Pearson Sales User OneCRM'].Id;
    List<User> ULst = TestDataFactory.createUser(ProfileId);
    user u = ULst[0];

    System.runAs(u) {
        CasepopulateDefault CaseDefaultInst = new CasepopulateDefault(sc);

    ULst[1].Line_of_Business__c = 'Professional';

    System.runAs(ULst[1]) {
        CasepopulateDefault CaseDefaultInst = new CasepopulateDefault(sc);

    ULst[2].Line_of_Business__c = '';

    System.runAs(ULst[2]) {
        CasepopulateDefault CaseDefaultInst = new CasepopulateDefault(sc);
    ULst[3].Line_of_Business__c = 'Higher Ed';
      System.runAs(ULst[3]) {
        CasepopulateDefault CaseDefaultInst = new CasepopulateDefault(sc);


please find below class

public class CasepopulateDefault{

    public CasepopulateDefault(ApexPages.StandardController controller){}

    public pagereference Redirect()
        PageReference pageRef;
        //To get the logged in userId and line of business value
        User u = [select Id, username, Line_of_Business__c from User where Id = :UserInfo.getUserId()];

        //read BV from cust setting
        string busVer;
         if(u.Line_of_Business__c !=null) {
        //string busVer='Higher Education';
        Map<String,String> parMap = Apexpages.currentPage().getparameters();
        //  System.debug('parMap'+parMap);
        String id= parMap.get('RecordType');
        System.debug('id of '+id);
        //  Query the Tech Support Record Type Id
        string recTypeID=Apexpages.currentPage().getparameters().get('RecordType');

        recordtype recordtypeInst = [select Id,Name  from recordtype where ID=:recTypeID and sobjecttype = 'Case'];

        if(recordtypeInst.Name=='Technical Support' && busVer !=null ) {
             pageRef = new PageReference('/500/e?retURL=%2F500%2Fo&RecordType='+recTypeID+'&ent=Case&00Nb000000AGUYm='+ busVer+ '&nooverride=1');


        else {
             pageRef = new PageReference('/500/e?retURL=%2F500%2Fo&RecordType='+recTypeID+'&ent=Case&nooverride=1');

        return pageRef;
  • 1
    Can you post your apex class also? – Reshma Apr 7 '17 at 10:06
  • Yes @Reshma added – Abraham Apr 7 '17 at 10:10

Below line: busVer=CS_user_BV_Map__c.getInstance(u.Line_of_Business__c).BV_Value__c; is accessing a custom setting record which you haven't defined in your test class. That is why you're getting a null pointer exception.

The workaround for this could be to create a custom setting record in your test class and then test it. This a standard practice to setup data for your controller and then test it.

| improve this answer | |
  • I tried adding if(CS_user_BV_Map__c.getInstance(u.Line_of_Business__c).BV_Value__c != null) to the controller but i get the same error Rohit.. – Abraham Apr 7 '17 at 11:19
  • @abraham it would be better to create setup data properly. That is the proper way to avoid the error. – Rohit Mourya Apr 7 '17 at 11:28

Use the below code snippet before "System.runAs(u) { "[line no.20] in CasepopulateDefaultTest class. Please put proper value to create custom setting.

List<CS_user_BV_Map__c> CS_user_BV_List = new List<CS_user_BV_Map__c>();

CS_user_BV_Map__c bVMap1 = new CS_user_BV_Map__c();
bVMap1.name = 'Professional';
bVMap1.Value__c  = 'your value here';

CS_user_BV_Map__c bVMap2 = new CS_user_BV_Map__c();
bVMap2.name = 'Higher Ed';
bVMap2.Value__c  = 'your value here';

insert CS_user_BV_List;
| improve this answer | |
  • Please donot forget to put desire value in bVMap.Value__c = ''; before inserting CS_user_BV_List – Subhasis Kar Apr 7 '17 at 10:46

your code fails on the profile id! only standard profiles are available

otherwise you need the non best practice @isTest(seealldata=true) but this is NOT the way to do it

if you need this profile to work with , the you have to create it in the test class(i wouldn't) just take standard

| improve this answer | |

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.