You need to sign in to do that
Don't have an account?

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
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
And test 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
Without changing the main class you cannt able to cover the test class
Hope this works,
Thanks
@ Ashishk
The above code will not work .. because to set the value you need to change the variable to set and get
No need we can access the variables directly using class.variable, as those are global variables.
check below, it worked for me