0

I am writing the test class for StandardSetController for Selected Records(Records are get Selected when SelectRecords from Checkbox using ListView). So In my test class, it is giving me an error of 'Attempt to De-reference a null object'. Help me into this

public with sharing class GlobalApprovalPageExtension {

    public GlobalApprovalPageExtension(ApexPages.StandardController controller) {

    }

      private ApexPages.StandardSetController standardController;
      public List<SCORE_Cost_Savings__c> ListSelectedScoreCost {get;set;} 
      public Integer TotalSubmittedValues {get;set;}

        public GlobalApprovalPageExtension(ApexPages.StandardSetController standardController) {

            this.standardController = standardController;
            ListSelectedScoreCost = new List<SCORE_Cost_Savings__c>();

           TotalSubmittedValues =  [SELECT count() FROM SCORE_Cost_Savings__c WHERE Status__c='Regional Approved'];

                system.debug('TotalSubmittedValues---------------->'+TotalSubmittedValues);

            List<SCORE_Cost_Savings__c> selectedSubmitted = new List<SCORE_Cost_Savings__c>();
            selectedSubmitted = (List<SCORE_Cost_Savings__c>) standardController.getSelected();
            system.debug('set------->>'+selectedSubmitted);
            SET<ID> SelectedID = new Set<ID>();
            system.debug('selectedSubmitted------------->'+selectedSubmitted);
            for(SCORE_Cost_Savings__c objScore:selectedSubmitted){
                        SelectedID.add(objScore.id);
            }
            system.debug('---------------->'+SelectedID);

           ListSelectedScoreCost = [SELECT id,Name,Status__c FROM SCORE_Cost_Savings__c WHERE Status__c='Regional Approved' AND ID IN:SelectedID];

           if(ListSelectedScoreCost.size()==0){

               apexpages.addMessage(new Apexpages.Message(ApexPages.severity.Info, 'Kindly Select the Scores from List View'));
           } 

        }
        //This method is for Updating the Status of Score to Global Approved
        public void UpdateStatus(){

            try{
               system.debug('ListUpdateStausScore----------->'+ListSelectedScoreCost);
                   List<SCORE_Cost_Savings__c> ListUpdateStausScore = new List<SCORE_Cost_Savings__c>();
                   for(SCORE_Cost_Savings__c objScore:ListSelectedScoreCost){
                       objScore.Status__c = 'Global Approved';
                       ListUpdateStausScore.add(objScore);
                   } 

                   if(ListUpdateStausScore.size()>0){

                       Update ListUpdateStausScore;

                       apexpages.addMessage(new Apexpages.Message(ApexPages.severity.Confirm, 'Score Status Updated to Global Approved'));

                   }

            }catch(Exception e){

                apexpages.addMessage(new Apexpages.Message(ApexPages.severity.ERROR, 'Exception due to ----->'+e.getMessage()+e.getLineNumber()));
                system.debug('Error Messages------------->'+e.getMessage()+e.getLineNumber());
            }

        }

}

Test Class

@isTest
public class GlobalApprovalPageExtensionTest {

    //Global variables
    private static SCORE_Cost_Savings__c objSavings;
    public Integer TotalSubmittedValues;
    private static SCORE_Cost_Savings__c objSaving;
    private static List<SCORE_Cost_Savings__c> slist = new List<SCORE_Cost_Savings__c>();
    private static List<SCORE_Cost_Savings__c> slist1 = new List<SCORE_Cost_Savings__c>();

    //test data setup method
    @testsetup public static void testData(){
        objSavings = new SCORE_Cost_Savings__c(Code__c='224883',
                                               Entry_Date__c=System.Today(),
                                               Score_Date__c=System.Today(),
                                               Event_Name__c='Test1',
                                               Event_Start__c=System.Today(),
                                               Negotiated_Cost_in_Local_Currency__c=800.00,
                                               Country__c='Americas',
                                               Currency_Type__c='AED',
                                               Description_of_Cost_Savings__c='This is test',
                                               Event_End__c=System.Today(),
                                               Starting_Cost_in_Local_Currency__c=1000.00,
                                               Status__c='Regional Approved'
                                              );


        insert objSavings;


        objSaving = new SCORE_Cost_Savings__c(Code__c='224883',
                                              Entry_Date__c=System.Today(),
                                              Score_Date__c=System.Today(),
                                              Event_Name__c='Test1',
                                              Event_Start__c=System.Today(),
                                              Negotiated_Cost_in_Local_Currency__c=800.00,
                                              Country__c='Americas',
                                              Currency_Type__c='AED',
                                              Description_of_Cost_Savings__c='This is test',
                                              Event_End__c=System.Today(),
                                              Starting_Cost_in_Local_Currency__c=1000.00,
                                              Status__c='Regional Approved');

        insert objSaving; 

            }

    //First method to validate the Extension class to Update the Status of Score to Regional Approved.
    public static testmethod void validateGlobalApprovalPageExtensionOne(){
        testData();



        // Set Id of score cost saving to current page
        //ApexPages.currentPage().getParameters().put('id', objSaving.Id);
        //objSaving.setSelected(slist); 
        slist.add(objSaving);

        //Instance of class
        ApexPages.StandardSetController standardController = new ApexPages.StandardSetController(slist);
        standardController.setSelected(slist);
        standardController.getSelected();
        ApexPages.StandardController sc = new ApexPages.StandardController(objSaving); 
        GlobalApprovalPageExtension objGlobalApprovalPageExtension = new GlobalApprovalPageExtension(sc); 
        GlobalApprovalPageExtension objGlobalApproval = new  GlobalApprovalPageExtension(standardController);
        objGlobalApprovalPageExtension.UpdateStatus();  


        //System.assertEquals('Global Approved',objSavings.Status__c); 
    }


    //Second method to validate the Extension class to Update the Status of Score to Regional Approved.
    public static testMethod void validateRegionalApprovalPageExtensionTwo(){  
         testData();


        // Set Id of score cost saving to current page
        //ApexPages.currentPage().getParameters().put('id', objSavings.Id);
        //objSaving.setSelected(slist); 
        slist.add(objSavings);
        //Instance of class
        ApexPages.StandardSetController standardController = new ApexPages.StandardSetController(slist);
        standardController.setSelected(slist);

        ApexPages.StandardController sc = new ApexPages.StandardController(objSavings); 
        GlobalApprovalPageExtension objGlobalApprovalPageExtension = new GlobalApprovalPageExtension(sc);        
        GlobalApprovalPageExtension objGlobalApproval = new  GlobalApprovalPageExtension(standardController);
           standardController.getSelected();
        objGlobalApprovalPageExtension.UpdateStatus();   
         //objSaving.Status__c='Global Approved';
        //System.assertEquals('Global Approved',objSavings.Status__c);  

    }

}
2
  • What is the line number or statement throwing this error? Commented Oct 20, 2016 at 9:59
  • On this Line for(SCORE_Cost_Savings__c objScore:ListSelectedScoreCost){ Commented Oct 20, 2016 at 10:00

1 Answer 1

1

That error means your ListSelectedScoreCost property is still null. If you look at your test, you construct both an ApexPages.StandardController and an ApexPages.StandardSetController. But you pas the former to your extension, and your StandardController constructor does not do anything. It actually seems like you should remove that constructor entirely unless you plan to define logic there as well.

Assuming you set your StandardSetController up correctly, you can just pass it to the extension and remove the StandardController.

Works

ApexPages.StandardSetController controller = new ApexPages.StandardSetController(...);
MyExtension extension = new MyExtension(controller);

Fails

ApexPages.StandardController controller = new ApexPages.StandardController(...);
MyExtension extension = new MyExtension(controller);

You must log in to answer this question.

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