function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhriti GhoshDhriti Ghosh 

System.NullPointerException: Argument cannot be null. in test class

Hi,

I am getting error while parsing datetime field in test class.My controller is working fine:
As I have given below code:
public pagereference saveOpportunity(){
String bcontact='';
String serializedDate = ApexPages.currentPage().getParameters().get('bcontact');
        System.debug('*************'+serializedDate);
       
        if(serializedDate!=''){
         dt = DateTime.parse(serializedDate);
         System.debug('*************'+dt);
        }
}
In test class I am writing :
Datetime nowdate=System.now();
        string strfromdate = nowdate.format();
        system.debug('startdate :' + strfromdate );
        datetime startdate = datetime.parse(strfromdate);
        system.debug('startdate :' + startdate);
        oppn.Best_Time_To_Contact__c=startdate ;
I am able to fetch oppn.Best_Time_To_Contact__c properly and also able to insert opportunity record.But when I am calling apex function  dScript.saveOpportunity(),
I am getting this error:

System.NullPointerException: Argument cannot be null.
Can anybody please help me where I am getting issue and how to resolve this issue ASAP.
Vishant ShahVishant Shah
You cannot call the function directly, you have to instantiate a pagereference and then call the save function of the pagereference

see here http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm
Dhriti GhoshDhriti Ghosh
Hi Vishant ,Thanks for your quick response.

So instead of calling the function directly I am writing now:
String nextPage = dScript.saveOpportunity().getUrl();
        nextPage = dScript.saveOpportunity().getUrl();
but still I am facing same error.
where dScript is the class instance,saveOpportunity is the function
Can you please tell me where i am doing wrong?
Vishant ShahVishant Shah
Hi Dhriti

can you please post you class and test method both so i can pin point what you need doing

Ta
Vish
Vishant ShahVishant Shah
basically you need to set 

PageReference pageRef = Page.YOURPAGENAMEHERE;
Test.setCurrentPage(pageRef);

thecontroller dScript= new thecontroller();
dScript.saveOpportunity();
Dhriti GhoshDhriti Ghosh
  Apex class method:
public pagereference saveOpportunity(){ 
        oppor = new Opportunity();
        String fnames='';
        String lnames='';
        String emails='';
        String phones='';
        String commentopportunity='';
        String bcontact='';
        String fname=Apexpages.currentpage().getparameters().get('fnames');
        String lname=Apexpages.currentpage().getparameters().get('lnames');
        String emailid=Apexpages.currentpage().getparameters().get('emails');
        String phonenumber=Apexpages.currentpage().getparameters().get('phones');
        String comment=Apexpages.currentpage().getparameters().get('commentopportunity');
        String serializedDate = ApexPages.currentPage().getParameters().get('bcontact');  //Values coming from actionfunction VF page
        System.debug('*************'+serializedDate);
       
        if(serializedDate!=''){
         dt = DateTime.parse(serializedDate);//Facing issue in this line while running the test class 
line 377
         System.debug('*************'+dt);
        }

        Decider_Interview__c deciderDetails=[Select Id from Decider_Interview__c where Id =:deciderInterviewDetails.id];
        Id accountId=Apexpages.currentpage().getparameters().get('id');
        Question_Response__c questionresponsedetails =[Select Id,Choices__c,isAccount__c,Question__c,isCheckedOpportunity__c,isCheckedAccount__c,isOpportunity__c from Question_Response__c where Id =:typeName];
        questiondetail=[Select id,Lead_Type__c from Question__c where id=:questionresponsedetails.Question__c];
        oppor.AccountId=accountId;
        oppor.Comment_for_family_member__c=comment;
        oppor.first_name__c = fname;
        oppor.last_name__c = lname;
        oppor.email__c = emailid;
        oppor.phone__c = phonenumber;
        oppor.CloseDate=System.today();
        oppor.Domain__c='Legal';
        oppor.Lead_Type__c=questiondetail.Lead_Type__c;
        oppor.Name=questiondetail.Lead_Type__c;
        oppor.StageName='Created';
        oppor.Sub_Domain__c='Legal Healthcare';
        oppor.Opportunity_Type__c=questionresponsedetails.choices__c;
        oppor.Affected_Person__c='Family Member';
        oppor.Best_Time_To_Contact__c=dt;
        oppor.Description=comment;
        try{
            insert oppor;
            opplist.add(oppor);
        }
      
        catch(DMLException e){
            System.debug('********************'+e);
        }
        notes = new note();
        notes.parentId=deciderDetails.Id;
        notes.body=comment;
        notes.title='Family notes';
        insert notes;
         return null;
         return null;
}



Test class:
@isTest(seeAllData = false)
Public class TestDeciderScript {
    public static testMethod void testDecider() {
       
        //Inserting Template record
        Template__c template = new Template__c();
        template.Name = 'Test Template';
        template.Interview_Type__c = 'Decider';
        template.Lead_Type__c = 'Actos';
        insert template;
       
        //Inserting Question Record
        Question__c question1 = new Question__c();
        question1.Name = 'Test Question';
        question1.Template__c = template.id;
        question1.isActive__c = true;
        question1.Order_Number__c = 1;
        question1.Question__c = '1.Have you or any of your loved ones ever been diagnosed with.';
        question1.Answer_Type__c = 'Multi-Select--Vertical';
        question1.isDependent__c = false;
        question1.Lead_Type__c = 'Birthcontrol';
        insert question1;
       
        //Inserting Question Record
        Question__c question2 = new Question__c();
        question2.Name = 'Test Question';
        question2.Template__c = template.id;
        question2.isActive__c = true;
        question2.Order_Number__c = 1;
        question2.Question__c = '1.Have you or any of your loved ones ever been diagnosed with.';
        question2.Answer_Type__c = 'Single Select--Vertical';
        question2.isDependent__c = false;
        question2.Lead_Type__c = 'Birthcontrol';
        insert question2;
       
        //Inserting Question Record
        Question__c question3 = new Question__c();
        question3.Name = 'Test Question';
        question3.Template__c = template.id;
        question3.isActive__c = true;
        question3.Order_Number__c = 1;
        question3.Question__c = '1.Have you or any of your loved ones ever been diagnosed with.';
        question3.Answer_Type__c = 'Free Text';
        question3.isDependent__c = false;
        question3.Lead_Type__c = 'Birthcontrol';
        insert question3;
       
        //Inserting Question Response Record
        Question_Response__c response1 = new Question_Response__c();
        response1.Name = 'Response';
        response1.Question__c = question1.Id;
        response1.Choices__c = 'Bladder Cancer from the use of Actos medication for diabetes [ACTOS]';
        response1.isAccount__c = true;
        response1.isCheckedAccount__c = false;
        response1.isOpportunity__c = false;
        response1.isOpportunityMe__c = false;
        response1.Response_Order_No__c = ''+1;
       
        insert response1;
       
        //Inserting Question Response Record
        Question_Response__c response2 = new Question_Response__c();
        response2.Name = 'Response';
        response2.Question__c = question1.Id;
        response2.Choices__c = 'Bladder Cancer from the use of Actos medication for diabetes [ACTOS]';
        response2.isAccount__c = true;
        response2.isCheckedAccount__c = true;
        response2.isOpportunity__c = false;
        response2.isOpportunityMe__c = false;
        response2.Response_Order_No__c = ''+1;
       
        insert response2;
       
        //Inserting Question Response Record
        Question_Response__c response3 = new Question_Response__c();
        response3.Name = 'Response';
        response3.Question__c = question1.Id;
        response3.Choices__c = 'Bladder Cancer from the use of Actos medication for diabetes [ACTOS]';
        response3.isAccount__c = false;
        response3.isCheckedOpportunity__c = false;
        response3.isOpportunity__c = true;
        response3.isOpportunityMe__c = false;
        response3.Response_Order_No__c = ''+1;
       
        insert response3;
       
        //Inserting Question Response Record
        Question_Response__c response4 = new Question_Response__c();
        response4.Name = 'Response';
        response4.Question__c = question1.Id;
        response4.Choices__c = 'Bladder Cancer from the use of Actos medication for diabetes [ACTOS]';
        response4.isAccount__c = false;
        response4.isCheckedOpportunity__c = true;
        response4.isOpportunity__c = true;
        response4.isOpportunityMe__c = false;
        response4.Response_Order_No__c = ''+1;
       
        insert response4;
       
        //Inserting Question Response Record
        Question_Response__c response5 = new Question_Response__c();
        response5.Name = 'Response';
        response5.Question__c = question1.Id;
        response5.Choices__c = 'Bladder Cancer from the use of Actos medication for diabetes [ACTOS]';
        response5.isAccount__c = false;
        response5.isCheckedMe__c = false;
        response5.isOpportunity__c = false;
        response5.isOpportunityMe__c = true;
        response5.Response_Order_No__c = ''+1;
       
        insert response5;
       
        //Inserting Question Response Record
        Question_Response__c response6 = new Question_Response__c();
        response6.Name = 'Response';
        response6.Question__c = question1.Id;
        response6.Choices__c = 'Bladder Cancer from the use of Actos medication for diabetes [ACTOS]';
        response6.isAccount__c = false;
        response6.isCheckedMe__c = true;
        response6.isOpportunity__c = false;
        response6.isOpportunityMe__c = true;
        response6.Response_Order_No__c = ''+1;
       
        insert response6;
       
       
        //Id RecId = [SELECT Id, Name, IsPersonType FROM RecordType
                    //WHERE sObjectType = 'Account' AND
                   // IsPersonType=True AND RecordType.Name = 'Person Account'].Id;
        RecordType RecId  = [select id,Name from RecordType where SobjectType='Account' and Name='Clients' Limit 1];
       
        //Inserting Account Record
        Account acc = new Account();
        acc.recordTypeId = RecId.Id;
        acc.FirstName = 'Test LastName';
        acc.LastName = 'Test LastName';       
       
        insert acc;
        System.debug('************acc**************8'+acc);
        Opportunity oppn = new Opportunity();
        oppn.Name = 'Test Opportunity';
        oppn.AccountId = acc.Id;
        oppn.StageName = 'Created';
        oppn.CloseDate = System.Today();
        //oppn.Best_Time_To_Contact__c=System.now();
        Datetime nowdate=System.now();                  
        string strfromdate = nowdate.format();
        system.debug('startdate :' + strfromdate );
        datetime startdate = datetime.parse(strfromdate);    //parsing from string to datetime
        system.debug('startdate :' + startdate);
        oppn.Best_Time_To_Contact__c=startdate ;

        insert oppn;
        system.debug('opportunity:'+oppn);
       
        Decider_Interview__c deciderInterview1 = new Decider_Interview__c();
        //deciderInterview1.Name
        deciderInterview1.Account_Name__c = acc.Id;
        deciderInterview1.Opportunity_Name__c = oppn.Id;
        insert deciderInterview1;
       
        Decider_Interview__c deciderInterview2 = new Decider_Interview__c();
        deciderInterview2.Account_Name__c = acc.Id;
        deciderInterview2.Opportunity_Name__c = oppn.Id;
        //deciderInterview2.Comment__c = 'Test Comment';
        insert deciderInterview2;
       
        Decider_Interview_Response__c dIntResponse1 = new Decider_Interview_Response__c();
        //dIntResponse1.Name = 'Decider Interview Response';
        dIntResponse1.Decider_Interview__c = deciderInterview1.Id;
        dIntResponse1.Question__c = question1.Id;
        insert dIntResponse1;
       
        Decider_Interview_Response__c dIntResponse2 = new Decider_Interview_Response__c();
        //dIntResponse2.Name = 'Decider Interview Response';
        dIntResponse2.Decider_Interview__c = deciderInterview2.Id;
        dIntResponse2.Question__c = question2.Id;
        insert dIntResponse2;
               
        PageReference pageRef = Page.DeciderScript;      //Declaring pageref
        Test.setCurrentPage(pageRef);
       

        ApexPages.currentPage().getParameters().put('Id', acc.Id);
        ApexPages.currentPage().getParameters().put('type', 'Decider');
               
        DeciderScript dScript = new DeciderScript();    //Creating class instance
       
        dScript.saveDecider();
        dScript.getContact();
        dScript.closePopup();
        dScript.showPopup();
        dScript.cancelAccount();
        dScript.cancelOpportunity();
        //dScript.updateCheckbox();
        dScript.getInterest();
        dScript.value = 'Yes';
        dScript.interestResponse();
        dScript.value = 'No';
        dScript.interestResponse();       
       
        dScript.interestValue = 'No';
        dScript.saveDecider();
        //dScript.updateComment();
       
        dScript.interestValue = 'Yes';
        dScript.saveDecider();
       
        //dScript.updateComment();
        dScript.customSave();
       
        ApexPages.currentPage().getParameters().put('Val', response1.Id);
        dScript.affectedMemberName();
       
        ApexPages.currentPage().getParameters().put('Val', response2.Id);
        dScript.affectedMemberName();
       
        ApexPages.currentPage().getParameters().put('Val', response3.Id);
        dScript.affectedMemberName();
       
        ApexPages.currentPage().getParameters().put('Val', response4.Id);
        dScript.affectedMemberName();
       
        ApexPages.currentPage().getParameters().put('Val', response5.Id);
        dScript.affectedMemberName();
       
        ApexPages.currentPage().getParameters().put('Val', response6.Id);
        dScript.affectedMemberName();
       
        dScript.diComment = 'Decider Comment';
        //dScript.updateComment();
        dScript.updateCheckbox();
        dScript.previous();
        dScript.previousThirdpage();
        dScript.customCancel();   
        dScript.customSaveNew(); 
        
        dScript.saveOpportunity();      //calling the function line 263
        dScript.saveOpportunityMe();
        dScript.thirdPage();
       
        dScript.bcontact = System.Now();
        ApexPages.currentPage().getParameters().put('bcontact', ''+dScript.bcontact);
        //dScript.dateFromUrl();
       
       
       
    }
}


Error Message System.NullPointerException: Argument cannot be null.
Stack Trace Class.DeciderScript.saveOpportunity: line 377, column 1
Class.TestDeciderScript.testDecider: line 263, column 1
Vishant ShahVishant Shah
I don't see you setting contact as a parameter in your test class. Set it before you call saveopportunity and you shudder be good I think. 

Vish
Vishant ShahVishant Shah
these 2 lines in your test code at the bottom should be above save opportunity

dScript.bcontact = System.Now();
        ApexPages.currentPage().getParameters().put('bcontact', ''+dScript.bcontact);