+ Start a Discussion
rupesh ranjanrupesh ranjan 

Error In Test Class kindly PLs any one help me out urgent basis

Error MessageSystem.NullPointerException: Argument cannot be null.
Stack TraceClass.sendmailp.sendpost: line 65, column 1
Class.sendmailcontacttest.runTest1: line 25, column 1  
                      &
Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Stack TraceClass.sendmailp.callapi2: line 1164, column 1
Class.sendmailcontacttest.runTest3: line 49, column 1
 
Test Class

@isTest
public class sendmailcontacttest{  
    static testMethod void runTest1() {
    Account account = new Account();
       account.Name = 'Acc3';
       insert account;         

       Contact contact = new Contact();
       contact.FirstName = 'Todda';
       contact.LastName = 'Wilmss';
       contact.AccountID = account.Id;
       contact.Email = 'twilms@verisign.conm';
       contact.MobilePhone = '918527116723';
       insert contact; 
            
         test.startTest();
         PageReference pageRef = Page.sendmail; // Your page name
         pageRef.getParameters().put('Id', String.valueOf(Contact.Id));
         Test.setCurrentPage(pageRef);
        Test.setMock(HttpCalloutMock.class, new sendmailcontactmock());
          ApexPages.StandardController sc1 = new ApexPages.StandardController(Contact);//Instance of your object
         sendmailp testDObj = new sendmailp();
          testDObj.sendpost();
           test.stopTest();  
            
    }   
    static testMethod void runTest3() {
    Account account = new Account();
       account.Name = 'Acc3';
       insert account;         

       Contact contact = new Contact();
       contact.FirstName = 'Todda';
       contact.LastName = 'Wilmss';
       contact.AccountID = account.Id;
       contact.Email = 'twilms@verisign.conm';
       contact.MobilePhone = '918527116723';
       insert contact; 
            
         test.startTest();
         PageReference pageRef = Page.Leadpage; // Your page name
         pageRef.getParameters().put('Id', String.valueOf(Contact.Id));
         Test.setCurrentPage(pageRef);
        Test.setMock(HttpCalloutMock.class, new sendmailcontactmock());
          ApexPages.StandardController sc1 = new ApexPages.StandardController(Contact);//Instance of your object
         sendmailp testDObj = new sendmailp();
          testDObj.callapi2();
           test.stopTest();  
            
    }
    static testMethod void runTest4() {
    Account account = new Account();
       account.Name = 'Acc3';
       insert account;         
       Contact contact = new Contact();
       contact.FirstName = 'Todda';
       contact.LastName = 'Wilmss';
       contact.AccountID = account.Id;
       contact.Email = 'twilms@verisign.conm';
       contact.MobilePhone = '918527116723';
       insert contact; 
	   test.startTest();
         PageReference pageRef = Page.Leadpage; // Your page name
         pageRef.getParameters().put('Id', String.valueOf(Contact.Id));
         Test.setCurrentPage(pageRef);
        Test.setMock(HttpCalloutMock.class, new sendmailcontactmock());
          ApexPages.StandardController sc1 = new ApexPages.StandardController(Contact);//Instance of your object
         sendmailp testDObj = new sendmailp();
          testDObj.getperformcallout();
           test.stopTest();              
    }  
}
 
Mock Class

@IsTest(seeAllData = false)
global class sendmailcontactmock implements HttpCalloutMock 
{   
    global HTTPResponse respond(HTTPRequest req) {        
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('[{"foo":"bar"}]');
        res.setStatusCode(200);        
        return res;
    }   
}
 
Main Class

Public  class sendmailp{

    public String to{get; set;}
    public String AddRecep{get; set;}
    public String subject {get; set;}
    public String body {get; set;}
    public String table3{get; set;}
     public String toAddresses {get; set;}
     public   String MySessionID = UserInfo.getSessionID();
     public String  conId = ApexPages.currentPage().getParameters().get('Id');
     public  String  EmailCon =[Select email from Contact where Id=:conId ].email;
     public   String myurl= URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/c/10.0/' + UserInfo.getOrganizationId();    
     public   string c='eU9WzoFgU4n8Apu5PYxcNGRZswRDZJWDEMdbQVU85gw=';
     public   String Acode= Userinfo.getuserid();
     public   String Oid = UserInfo.getOrganizationId();
    Blob cryptoKey= EncodingUtil.base64Decode(c);
    Blob data = Blob.valueOf(MySessionID );
    Blob data1 = Blob.valueOf(myurl);
    Blob data2 = Blob.valueOf(conId);
    Blob data3 = Blob.valueOf(EmailCon);
    Blob data4 = Blob.valueOf(Acode);
    Blob data5 = Blob.valueOf(Oid);
    Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
    Blob encryptedData1 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data1);
    Blob encryptedData2 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data2);
    Blob encryptedData3 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data3 );
    Blob encryptedData4 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data4 );
    Blob encryptedData5 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data5 );

    public    String b64Data = EncodingUtil.base64Encode(encryptedData);
    public    String b64Data1 = EncodingUtil.base64Encode(encryptedData1);
    public    String b64Data2 = EncodingUtil.base64Encode(encryptedData2);
    public    String b64Data3 = EncodingUtil.base64Encode(encryptedData3);
    public    String b64Data4 = EncodingUtil.base64Encode(encryptedData4);
    public    String b64Data5 = EncodingUtil.base64Encode(encryptedData5);
 
    public String email = EncodingUtil.urlEncode(b64Data3, 'UTF-8');    
    public String aucode= EncodingUtil.urlEncode(b64Data4, 'UTF-8');
    public String ocode= EncodingUtil.urlEncode(b64Data5, 'UTF-8');

    public sendmailp(ApexPages.StandardController controller) {
    AddRecep = '';
    body=apexpages.currentpage().getparameters().get('att');   
           
    }
    
   public void sendpost(){
    String MySessionID = UserInfo.getSessionID();
      String  conId = ApexPages.currentPage().getParameters().get('Id');
      String  EmailCon =[Select email from Contact where Id=:conId ].email;
        String myurl= URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/c/10.0/' + UserInfo.getOrganizationId();    
        string c='eU9WzoFgU4n8Apu5PYxcNGRZswRDZJWDEMdbQVU85gw=';
        String Acode= Userinfo.getuserid();
        String Oid = UserInfo.getOrganizationId();
    Blob cryptoKey= EncodingUtil.base64Decode(c);
    Blob data = Blob.valueOf(MySessionID );
    Blob data1 = Blob.valueOf(myurl);
    Blob data2 = Blob.valueOf(conId);
    Blob data3 = Blob.valueOf(EmailCon);
    Blob data4 = Blob.valueOf(Acode);
    Blob data5 = Blob.valueOf(Oid);
   Blob data6 = Blob.valueOf(to);
   Blob data7 = Blob.valueOf(AddRecep);
  Blob data8 = Blob.valueOf(subject);
   Blob data9 = Blob.valueOf(body);
    Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
    Blob encryptedData1 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data1);
    Blob encryptedData2 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data2);
    Blob encryptedData3 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data3 );
    Blob encryptedData4 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data4 );
    Blob encryptedData5 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data5 );
   Blob encryptedData6 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data6 );
    Blob encryptedData7 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data7 );
    Blob encryptedData8 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data8 );
   Blob encryptedData9 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data9 );
        String b64Data = EncodingUtil.base64Encode(encryptedData);
        String b64Data1 = EncodingUtil.base64Encode(encryptedData1);
        String b64Data2 = EncodingUtil.base64Encode(encryptedData2);
        String b64Data3 = EncodingUtil.base64Encode(encryptedData3);
        String b64Data4 = EncodingUtil.base64Encode(encryptedData4);
        String b64Data5 = EncodingUtil.base64Encode(encryptedData5);
       String b64Data6 = EncodingUtil.base64Encode(encryptedData6);
       String b64Data7 = EncodingUtil.base64Encode(encryptedData7);
        String b64Data8 = EncodingUtil.base64Encode(encryptedData8);
       String b64Data9 = EncodingUtil.base64Encode(encryptedData9);
    String testurl='sessionID='+EncodingUtil.urlEncode(b64Data, 'UTF-8')+'&serverUrl='+EncodingUtil.urlEncode(b64Data1, 'UTF-8')+'&contactID='+EncodingUtil.urlEncode(b64Data2, 'UTF-8')+'&email='+EncodingUtil.urlEncode(b64Data3, 'UTF-8');
     String email = EncodingUtil.urlEncode(b64Data3, 'UTF-8');    
     String aucode= EncodingUtil.urlEncode(b64Data4, 'UTF-8');
     String ocode= EncodingUtil.urlEncode(b64Data5, 'UTF-8');
    String too= EncodingUtil.urlEncode(b64Data6, 'UTF-8');
     String cccc= EncodingUtil.urlEncode(b64Data7, 'UTF-8');
     String sbj= EncodingUtil.urlEncode(b64Data8, 'UTF-8');
    String bdy= EncodingUtil.urlEncode(b64Data9, 'UTF-8');
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        req.setEndpoint('https://www.demomail.net/sf/api/SaveDemoMailPost');
        
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');        req.setBody('AUCode='+aucode+'&SOrgID='+ocode+'&Subject='+sbj+'&Body='+bdy+'&To='+too+'&CC='+cccc+'&Bcc='); 
system.debug('???'+aucode);
        try {
                res = http.send(req);
                if (res.getBody() != null) {
                    System.debug('#Success!');
                } else {
                    System.debug('HTTP error: ' + res.getStatusCode());
                }
                System.debug(res.getBody());
            } 
        catch(System.CalloutException e) 
            {
                System.debug('Callout error: '+ e);
            }
   }
        
    public List<wrapContact> wrapContactList {get; set;}
    public List<Contact> selectedContacts{get;set;}
    Public String selectId {get;set;}
    public sendmailp(){
        body = apexpages.currentpage().getparameters().get('att');
            if(wrapContactList == null) {
            wrapContactList= new List<wrapContact>();
            for(Contact a: [SELECT  Name,FirstName,LastName,Email,Title FROM Contact ORDER by Name ASC]) {
                wrapContactList.add(new wrapContact(a));
           }
        }
        normalList = true;
        selectedList = false;
        body = ''; 
       to = EmailCon; 
    }         
    public void processSelected() {
        AddRecep = '';
        selectedContacts = new List<Contact>();
        for(wrapContact wrapContactObj : wrapContactList) {
            if(wrapContactObj.selected == true) {
                selectedContacts.add(wrapContactObj.acc);
                if (AddRecep == '') {
                    AddRecep = wrapContactObj.acc.Email;
                } else {
                    AddRecep += ';' + wrapContactObj.acc.Email ;
                }
            }
        }
    }
    public class wrapContact {
        public Contact acc {get; set;}
        public Boolean selected {get; set;}
        public wrapContact (Contact a) {
            acc = a;
            selected = false;
        }
    } 
    
    public List<video> ConsoleWrapperList1{get;set;}
    public String consolewrap{get;set;}
    public String  SessionId = '';
    public void callapi2(){
    String  SessionId = '';
    string testSessionId = '';
    for( consolewrap cwt : ConsoleWrapperList){
        testSessionId += cwt.sid;
        if(cwt.selected){
            if(SessionId == ''){
                SessionId += cwt.SessionId;
            }else{
                SessionId += ','+ cwt.SessionId;
            }
        }
    } 
    system.debug('testSessionId+++'+testSessionId);
    List<video> ConsoleWrapperList1 = new List<video>();
    HttpRequest req = new HttpRequest(); 
    HttpResponse res = new HttpResponse();
    Http http = new Http(); 
    system.debug('SessionId+++'+SessionId);
   string url = 'https://www.demomail.net/sf/api/SendVideoContactPreview?AUCode='+aucode+'&SOrgID='+ocode+'&SessionIds='+SessionId;
      req.setEndpoint(url); 
    req.setMethod('GET');
    res = http.send(req);
    if(res.getstatusCode() == 200 && res.getbody() != null){ 
        String replaceIllegal= res.getbody().replaceAll('\n','').replaceAll('\r','');
        ConsoleWrapperList1=(List<video>)System.JSON.deserialize(replaceIllegal,List<video>.class);
        system.debug('ConsoleWrapperList1+++'+ConsoleWrapperList1);
        body = ConsoleWrapperList1.get(0).bodyT;
    }
}
    public String ssnid{get;set;}
    public boolean normalList{get;set;}
    public boolean selectedList{get;set;}
    public String Title{get;set;} 
    public Boolean selected {get; set;} 
    public List<consolewrap> ConsoleWrapperList{get;set;}
    public List<consolewrap> getperformcallout(){
        ConsoleWrapperList = new List<consolewrap>();
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
     string url = 'https://www.demomail.net/sf/api/SendVideoContact?AUCode='+aucode+'&SOrgID='+ocode+'&id='+SessionId;
        System.debug('##: '+ SessionId);
      req.setEndpoint(url);
        req.setMethod('GET');
        res = http.send(req);
        if(res.getstatusCode() == 200 && res.getbody() != null){
            ConsoleWrapperList=(List<consolewrap>)json.deserialize(res.getbody(),List<consolewrap>.class); 
            System.debug('value: '+ ConsoleWrapperList );
        }
        return consolewrapperlist;
    }
    
}

 
AnjithKumarAnjithKumar
Rupesh,

you have to assign values to the fields which you are using in your code. If you don't assign value to the fields then you will get the Null pointer exceptions.

Thanks,
Anjith kumar.
rupesh ranjanrupesh ranjan
Yes How i will insert VF field (AddRecep) in test class??
Mahesh DMahesh D
Sample code to set the values used in VF page for writting the Test Class:
 
ApexPages.currentPage().getParameters().put( 'id', quote.Id );
ApexPages.currentPage().getParameters().put( 'contactId', contactId );

Regards,
Mahesh
Naval Sharma4Naval Sharma4
Hi Rupesh Ranja,

There are 2 solutions for your problem

1) You need to intialize your AddRecap variable in your constructor. You are assignting it in standard controller construcor of sendmailp (Line No- 044) not in the non-paramerized constructor. So just put same statement after line no. 123. (Recommended solution)

2) You can also do this initialization before calling any method of sendmailp class. You just need to explicitly call setter for that variable just like below.
 testDObj.AddRecap = '';

Add this before line number 49.

I hope this will help you.

Thanks & Regards,
Naval