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
Maria22Maria22 

How to resolve null pointer exceptio:Argument cannot be null in test class

Hi Everyone,

I stuck at one place and could not able to resolve the issue.I know what is the issue and where its happening still I am clueless.Actually I am getting System.NullPointerException: Argument cannot be null in Test Class.

Below is my class:
 
01global with sharing class Audit {
02     
03    global String created_by;
04    global String reason;
05    global String requested_by_customer;
06    global String requested_by_type;
07    // Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
08    global Long requested_datetime;
09    global String requested_method;
10    global String others;
11    global String source_system;
12     
13     
14    global Consent_History__c getConsentHistory() {
15        Consent_History__c consentHistory = newConsent_History__c();
16         
17        consentHistory.Consent_Change_Created_By__c= this.created_by;
18        consentHistory.Change_Reason__c =this.reason;
19        consentHistory.Consent_Change_Requested_By_Customer__c= this.requested_by_customer;
20        consentHistory.Change_Received_From_Type__c= this.requested_by_type;
21        consentHistory.Change_Received_DateTime__c= DateTime.newInstance(this.requested_datetime);
22        consentHistory.Change_Received_Method__c= this.requested_method;
23        consentHistory.Consent_Change_Others__c =this.others;
24        consentHistory.Source_System__c =this.source_system;
25         
26        return consentHistory;
27    }
28}
Below is my test class:
01@isTest
02private class AuditTest {
03 
04    static testMethod void getConsentHistory() {
05       
06        Datetime myDate=System.now();
07         
08        Consent_History__c consentHistory = newConsent_History__c();
09       
10        consentHistory.Consent_Change_Created_By__c= 'User1@cochlear.com';
11        consentHistory.Change_Reason__c ='Test';
12        consentHistory.Consent_Change_Requested_By_Customer__c= 'Customer1';
13        consentHistory.Change_Received_From_Type__c='Professional';
14        consentHistory.Change_Received_DateTime__c= myDate;
15        consentHistory.Change_Received_Method__c= 'Consent Card';
16        consentHistory.Consent_Change_Others__c ='Consent Test';
17        consentHistory.Source_System__c=Constants.SYSTEM_MCP;
18       insert consentHistory;
19         
20       Test.startTest();
21       
22       Audit audit=new Audit();
23       audit.getConsentHistory();
24       Test.stopTest();
25         
26    }
27   
28}
I am receiving error at below line in class:
1consentHistory.Change_Received_DateTime__c =DateTime.newInstance(this.requested_datetime);
and below line in test class:
1audit.getConsentHistory();
I understand that there is something which I am not able to catch for date time in above class which results in returning null value.

Kindly help me to pass this error in my test class.

Any help will be greatly appreciated.

Many thanks in advance

Thanks & Regards,
Maria
Raj VakatiRaj Vakati
Change your class as below 
 
global with sharing class Audit {
   
    global String created_by{get;set;}
   global String reason{get;set;}
    global String requested_by_custome{get;set;}
    global String requested_by_type{get;set;}
    // Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
    global Long requested_datetime{get;set;}
    global String requested_method{get;set;}
    global String others{get;set;}
    global String source_system{get;set;}
    global Consent_History__c getConsentHistory() {
        Consent_History__c consentHistory = new Consent_History__c();
        consentHistory.Consent_Change_Created_By__c= this.created_by;
        consentHistory.Change_Reason__c =this.reason;
        consentHistory.Consent_Change_Requested_By_Customer__c= this.requested_by_customer;
        consentHistory.Change_Received_From_Type__c= this.requested_by_type;
        consentHistory.Change_Received_DateTime__c= DateTime.newInstance(this.requested_datetime);
        consentHistory.Change_Received_Method__c= this.requested_method;
        consentHistory.Consent_Change_Others__c =this.others;
        consentHistory.Source_System__c =this.source_system;
         
        return consentHistory;
    }
}


And test class
 
@isTest
private class AuditTest {
 
    static testMethod void getConsentHistory() {
        Datetime myDate=System.now();
         
        Consent_History__c consentHistory = newConsent_History__c();
       
        consentHistory.Consent_Change_Created_By__c= 'User1@cochlear.com';
        consentHistory.Change_Reason__c ='Test';
        consentHistory.Consent_Change_Requested_By_Customer__c= 'Customer1';
        consentHistory.Change_Received_From_Type__c='Professional';
        consentHistory.Change_Received_DateTime__c= myDate;
        consentHistory.Change_Received_Method__c= 'Consent Card';
        consentHistory.Consent_Change_Others__c ='Consent Test';
        consentHistory.Source_System__c=Constants.SYSTEM_MCP;
       insert consentHistory;
         
       Test.startTest();
       
       Audit auditCls=new Audit();
	   auditCls.created_by ='User1@cochlear.com' ;
	   auditCls.reason='Test';
	   auditCls.requested_by_custome ='Customer1';
	   auditCls.requested_by_type='Professional' ' 
	   auditCls.requested_datetime =System.now() ;
	   auditCls.requested_method='Consent Card';
	   auditCls.others='Test' ; 
	   auditCls.source_system='SAP';
	   
	   
       auditCls.getConsentHistory();
       Test.stopTest();
         
    }
}


 
Maria22Maria22
Thanks Raj for your quick response. I have worry in changing main Apex class. 
My functionality is absolutely working fine and just for getting test class pass I am afraid of changing my Apex class using get and set. 
May I know any particular reason of using get set in main class. There are some reasons why I didn't use get set in my class earlier. Also isn't there a way my test class get pass without changing anything in many class. 

Any help will be greatly appreciated. 

​​​Many thanks in advance
Raj VakatiRaj Vakati
Your main class changes dnt have any impact ..  

Without changing the main class you cannt able to cover the test class  
AshishkAshishk
Try below:-
@isTest
private class AuditTest {
 
    static testMethod void getConsentHistory() {        
         
       Test.startTest();
       
       Audit auditCls=new Audit();
	   auditCls.created_by ='User1@cochlear.com' ;
	   auditCls.reason='Test';
	   auditCls.requested_by_customer='Customer1';
	   auditCls.requested_by_type='Professional' ' 
	   auditCls.requested_datetime =System.now().getTime() ;
	   auditCls.requested_method='Consent Card';
	   auditCls.others='Test' ; 
	   auditCls.source_system='SAP';
	   
	   
       auditCls.getConsentHistory();
       Test.stopTest();
         
    }
}

Hope this works,
Thanks

 
Raj VakatiRaj Vakati

@ Ashishk

The above code will not work .. because to set the value you need to change  the variable to  set and get 

 
AshishkAshishk
@Raj Vakati

No need we can access the variables directly using class.variable, as those are global variables.

check below, it worked for me
global class WithoutGetSet {
	global string str;
    global string str1;
    
    public void arrangeValues(){
        str1=str;
    }
}



@istest
public class WithoutGetSetTest {

    static testmethod void test1(){
        WithoutGetSet o=new WithoutGetSet();
        o.str ='test';
        o.arrangeValues();
        system.assertEquals(o.str, o.str1);
    }
}