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

System.NullPointerException: Argument cannot be null in Test Class Salesforce
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:
Below is my test class:
I am receiving error at below line in class:
and below line in test class:
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:
global with sharing class Audit { global String created_by; global String reason; global String requested_by_customer; global String requested_by_type; // Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT global Long requested_datetime; global String requested_method; global String others; global String source_system; 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; } }
Below is my test class:
@isTest private class AuditTest { static testMethod void getConsentHistory() { Datetime myDate=System.now(); Consent_History__c consentHistory = new Consent_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 audit=new Audit(); audit.getConsentHistory(); Test.stopTest(); } }
I am receiving error at below line in class:
consentHistory.Change_Received_DateTime__c = DateTime.newInstance(this.requested_datetime);
and below line in test class:
audit.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
Replace
consentHistory.Change_Received_DateTime__c = DateTime.newInstance(this.requested_datetime);
line with below line :
consentHistory.Change_Received_DateTime__c = Test.isRunningTest() ? Date.Today() : DateTime.newInstance(this.requested_datetime);
Please like and mark this as best answer if you find it positive.
Thanks,
Jolly Birdi
Initialize the Variables in Audit after you have the instance creation in The test method. After that you can call the method in the actual class from test class.
Thanks,
Vivek
If possible can you pls elaborate a bit with code exactly what I am missing to achieve my desirable.
Any help will be greatly appreciated.
Many thanks in advance
I saw your comment and it seems to me that you wanted me to change line in main apex class with the one you suggested.
I am afraid whether I really need to change the line because my functionality works fine with the same code. Also I always give a second thought of using Test.isRunninv().
Isn't there a way to achieve my desirable without changing any line of code in main apex class. I mean is there any way I can have my test class pass without changing in main apex class.
Any help will be greatly appreciated.
Many thanks in advance
Sorry for the late response.
You can cover it up by adding the below line in the test class:
Audit.requested_datetime = 545896578;
Hope So, it would be helpful for you.
Thanks
Jolly Birdi
Thanks for your help.The suggestion provided by you works perfectly.
I appreciate your help.
Can you pls help me in one other issue which stuck me
I got stuck at one place where I am not able to cover a particular catch block consisting application exception. Currently my test class is getting pass succefully with 72% coverage but not able to cover one catch block.
Below is my class:
Below is my test class:
Below is the catch block which I am not able to cover:
Any help will be graeatly appreciated.
Many thanks in advance