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 

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:
 
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

​​​​​​​
Jolly_BirdiJolly_Birdi
Hello @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
Vivek S 42Vivek S 42
Hi Maria, 

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 
Maria22Maria22
Thanks Vivek for your response. 

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

 
Maria22Maria22
Thanks Jolly Bird for your response. I appreciate you were the first one to help on my question. 
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
Jolly_BirdiJolly_Birdi
Hello @Maria

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
Maria22Maria22
Hi 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:
@RestResource(urlMapping='/v1/someurl/*')
global with sharing class VerifiableAPI {
    
   private static BasicRestResponse response;
    
    @HttpPost
    global static void doPost(Request request) {
        Exception ex = null;
        Savepoint sp = Database.setSavepoint();
        CareService svr = null;
        
        try {
            svr =CareService(request, CareService.APIType.VERIFIABLE);
            svr.doPost();
            
            response = new RestResponseSuccess();
        } catch (ApplicationException e) {
            if (response == null) { response = new RestResponseError(); }
            response.setError('INVALID_REQUEST', e.getMessage());
            ex = e;
            Database.rollback(sp);
        } catch (Exception e) {
            if (response == null) { response = new RestResponseError(); }
            response.setError('INTERNAL_ERROR', e.getMessage());
            ex = e;
            Database.rollback(sp);
        }
        
        if (ex != null) {
            ApplicationLog.LogMessage('Error', 'CareVerifiableAPI', 'doPost', null, null, 
                                  JSON.serialize(response), JSON.serialize(RestContext.request), ex);
        }
        
      
        
        response.publish();
    }
    
    /**
     * Request of the API Call
     */
     global class Request {
        
         global String consent;
         
        
     }
}

Below is my test class:
@isTest
private class VerifiableAPITest {

    static testMethod void myUnitTest() {
      ;
       
           VerifiableAPI.Request reqst=new VerifiableAPI.Request();
        	reqst.consent='consent';
      
       
            String JsonMsg=JSON.serialize(reqst);
            Test.startTest();  
  		

           RestRequest req = new RestRequest(); 
           RestResponse res = new RestResponse();
        
            req.requestURI = '/services/apexrest/v1/demo';  //Request URL
            req.httpMethod = 'POST';//HTTP Request Type
            req.requestBody = Blob.valueof(JsonMsg);
            RestContext.request = req;
            RestContext.response= res;
        
           BasicRestResponse resp= new BasicRestResponse();
            resp.setError('title', 'detail');
            resp.setData(null);
            resp.getData();
            resp.publish();
            
        VerifiableAPI.doPost(reqst);
            Test.stopTest();
        
    }

}

Below is the catch block which I am not able to cover:
response = new RestResponseSuccess();
        } catch (ApplicationException e) {
            if (response == null) { response = new RestResponseError(); }
            response.setError('INVALID_REQUEST', e.getMessage());
            ex = e;
            Database.rollback(sp);
}

Any help will be gra​​​​​​eatly appreciated.

Many thanks in advance