1

Here I have a controller and test class, but I am not sure why I am getting the following error.

Error Message: System.NullPointerException: Argument cannot be null.

Stack Trace Class.CustomProductSelectionController.: line 36, column 1 Class.TestCustomProductSelectionController.myUnitTest: line 37, column 1

APEX CLASS

public CustomProductSelectionController(ApexPages.StandardController Controller){
    Product2Id = ApexPages.currentPage().getParameters().get('Product2Id');
    opp = new Opportunity();
    opp.Id = Apexpages.currentPage().getParameters().get('Id');
    opp.LID__LinkedIn_Company_Id__c = Apexpages.currentPage().getParameters().get('oppLID');
    opp.Name = Apexpages.currentPage().getParameters().get('oppName');
    opp.Accountid = Apexpages.currentPage().getParameters().get('oppAccountid');
    /* Below is my Line 36 */
    opp.CloseDate = Date.Parse(Apexpages.currentPage().getParameters().get('oppCloseDate'));
    opp.PrimaryTechnologyArchitect__c = Apexpages.currentPage().getParameters().get('oppPrimaryTech');
    opp.StageName = Apexpages.currentPage().getParameters().get('oppStageName');
    opp.Probability = Decimal.valueOf(Apexpages.currentPage().getParameters().get('oppProbability'));
    opp.Type = Apexpages.currentPage().getParameters().get('oppType');
    opp.WinLoss__c = Apexpages.currentPage().getParameters().get('oppWinLoss__c');
    opp.LeadSource = Apexpages.currentPage().getParameters().get('oppLeadSource');
    opp.Assessment__c = Apexpages.currentPage().getParameters().get('oppAssessment__c');
    opp.Budget__c = Apexpages.currentPage().getParameters().get('oppBudget');
    opp.ContractNumber__c = Apexpages.currentPage().getParameters().get('oppContractNumber');
    opp.Competition__c = Apexpages.currentPage().getParameters().get('oppCompetition');
    opp.Competition2__c = Apexpages.currentPage().getParameters().get('oppCompetition2');
    opp.Competition3__c = Apexpages.currentPage().getParameters().get('oppCompetition3');
    opp.SOW_Nbr__c = Apexpages.currentPage().getParameters().get('oppSOW');
    opp.BIG_Sow_Number__c = Apexpages.currentPage().getParameters().get('oppSowNumber');
    oppId = Apexpages.currentPage().getParameters().get('oppId');

    checkSelect = Apexpages.currentPage().getParameters().get('oppPrimaryTech');
    sowVal = Apexpages.currentPage().getParameters().get('oppSOW');

    wrappProductList = new List<wrappProduct>();
    product = new Product2[]{};
    AddProduct();
}

APEX TEST CLASS

@isTest
Public class TestCustomProductSelectionController{

    Private static testMethod void myUnitTest() {

        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;

        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.CloseDate = date.today();
        opp.StageName = 'Prospecting';
        opp.PrimaryTechnologyArchitect__c = 'Ryan';
        opp.AccountId = acc.Id;
        opp.LID__LinkedIn_Company_Id__c = '123';
        opp.Probability = 10;
        opp.Type = 'New Buisness';
        opp.WinLoss__c = '10';
        opp.LeadSource = 'EMC';
        opp.Assessment__c = 'Yes';
        opp.Budget__c = '500';
        opp.ContractNumber__c = '10';
        opp.Competition__c = 'CNT';
        opp.Competition2__c = 'CSC';
        opp.Competition3__c = 'ICP';
        opp.SOW_Nbr__c = '2';
        opp.BIG_Sow_Number__c = '1';
        insert opp;

        Product2 p = new Product2();
        p.Name = 'Test Product';
        p.Manufacturer__c = 'CDI Consulting';
        insert p;

        ApexPages.StandardController sc = new ApexPages.StandardController(opp);
        /* Below is my Line 37 */
        CustomProductSelectionController controller = new CustomProductSelectionController(sc);

        CustomProductSelectionController.wrappProduct wrapper = new CustomProductSelectionController.wrappProduct();

        controller.oppId = Apexpages.currentPage().getParameters().put('oppId', opp.Id);
        controller.Product2Id = ApexPages.currentPage().getParameters().put('Product2Id', p.Id);
        controller.opp.Id = Apexpages.currentPage().getParameters().put('Id', opp.Id);
        controller.opp.LID__LinkedIn_Company_Id__c = Apexpages.currentPage().getParameters().put('oppLID', opp.LID__LinkedIn_Company_Id__c);
        controller.opp.Name = Apexpages.currentPage().getParameters().put('oppName',opp.Name);
        controller.opp.Accountid = Apexpages.currentPage().getParameters().put('oppAccountid',opp.AccountId);
        controller.opp.CloseDate =  Apexpages.currentPage().getParameters().put('oppCloseDate', String.ValueOf(opp.CloseDate));
        controller.opp.PrimaryTechnologyArchitect__c = Apexpages.currentPage().getParameters().put('oppPrimaryTech',opp.PrimaryTechnologyArchitect__c);
        controller.opp.StageName = Apexpages.currentPage().getParameters().put('oppStageName',opp.StageName);
        //controller.opp.Probability = Apexpages.currentPage().getParameters().put('oppProbability', opp.Probability);
        controller.opp.Type = Apexpages.currentPage().getParameters().put('oppType',opp.Type);
        controller.opp.WinLoss__c = Apexpages.currentPage().getParameters().put('oppWinLoss__c',opp.WinLoss__c);
        controller.opp.LeadSource = Apexpages.currentPage().getParameters().put('oppLeadSource',opp.LeadSource);
        controller.opp.Assessment__c = Apexpages.currentPage().getParameters().put('oppAssessment__c',opp.Assessment__c);
        controller.opp.Budget__c = Apexpages.currentPage().getParameters().put('oppBudget',opp.Budget__c);
        controller.opp.ContractNumber__c = Apexpages.currentPage().getParameters().put('oppContractNumber',opp.ContractNumber__c);
        controller.opp.Competition__c = Apexpages.currentPage().getParameters().put('oppCompetition',opp.Competition__c);
        controller.opp.Competition2__c = Apexpages.currentPage().getParameters().put('oppCompetition2',opp.Competition2__c);
        controller.opp.Competition3__c = Apexpages.currentPage().getParameters().put('oppCompetition3',opp.Competition3__c);
        controller.opp.SOW_Nbr__c = Apexpages.currentPage().getParameters().put('oppSOW',opp.SOW_Nbr__c);
        controller.opp.BIG_Sow_Number__c = Apexpages.currentPage().getParameters().put('oppSowNumber',opp.BIG_Sow_Number__c);
        //controller.checkSelect = Apexpages.currentPage().getParameters().put('oppPrimaryTech',opp.IsPrivate);
        controller.sowVal = Apexpages.currentPage().getParameters().put('oppSOW',opp.BIG_Sow_Number__c);
        controller.AddProduct();
        controller.delWrapper();
        controller.SaveProduct();
        controller.retrieveProductInfo();
    }
}
3

The problem is that you are accessing the oppCloseDate parameter from the current page without assigning it, hence returning a null value. Date.parse() function can not take a null value as input.

You need to put the page parameters before calling the CustomProductSelectionController constructor.

    ApexPages.StandardController sc = new ApexPages.StandardController(opp);
    /*Put the page parameters here*/
    CustomProductSelectionController controller = new CustomProductSelectionController(sc);

Another problem I can see that you haven't set your page reference in your test class.

Test.setCurrentPage(Page.YourPageName);

Use it before initiating the standard controller to set your test method in perspective of your visualforce page.

|improve this answer|||||
  • you mean direct page Parameter put before calling the constructor ? like this only Apexpages.currentPage().getParameters().put('oppId', opp.Id); – WantToBeACoder Apr 12 '17 at 13:57
  • Yes, same code before calling the constructor. – Santanu Halder Apr 12 '17 at 14:05
  • added but getting the same error :( – WantToBeACoder Apr 12 '17 at 14:05
  • You have Apexpages.currentPage().getParameters().put('oppCloseDate', opp.CloseDate); this line commented in your test class. I hope that's not the case in your org. – Santanu Halder Apr 12 '17 at 14:07
  • yes. that's causing the error and i remove the comment and put type casting in opp.closeDate but getting the error : System.TypeException: Invalid date: 2017-04-12 – WantToBeACoder Apr 12 '17 at 14:11
2

If I understood it correct, your line number 36 is:

opp.CloseDate = Date.Parse(Apexpages.currentPage().getParameters().get('oppCloseDate'));

If yes, then looks like that you have to add one more line to your test class, where you need to set the oppCloseDate parameter as well.

Apexpages.currentPage().getParameters().put('oppCloseDate ',opp.closeDate);
|improve this answer|||||
  • i have already added this line see the Question again :) – WantToBeACoder Apr 12 '17 at 13:50
  • But I can see that line commented in your test class. And santanu's answer below has one more point, you have to use Test.setCurrentPage(Page.yourpagename); as well. – Nitish Singhal Apr 12 '17 at 13:54
  • one more thing..when i open this line i am getting the following error : Error: Compile Error: Incompatible value type Date for Map<String,String> at line 43 column 49 – WantToBeACoder Apr 12 '17 at 14:03
  • In this case, you can write Apexpages.currentPage().getParameters().put('oppCloseDate', String.valueOf(opp.CloseDate));, because it expects the second parameter to be string only. please try. – Nitish Singhal Apr 12 '17 at 14:04
  • what should i write Nitish ? – WantToBeACoder Apr 12 '17 at 14:07

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.