+ Start a Discussion
rupesh ranjanrupesh ranjan 

any one plss solve this Error in TEST CLASS

Error MessageSystem.NullPointerException: Argument cannot be null.
Stack TraceClass.sendmailexample.sendpost: line 62, column 1
Class.sendcontacttest.runTest1: line 34, column 1
                                      &
Error MessageSystem.QueryException: List has no rows for assignment to SObject
Stack TraceClass.sendmailexample.<init>: line 10, column 1
Class.sendcontacttest.runTest5: line 67, column 1
@isTest
public class sendcontacttest{  
    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;
              
        ApexPages.StandardController sc2 = new ApexPages.StandardController(Contact);
        apexpages.currentpage().getparameters().put('ID',contact.id);
        String  conId = ApexPages.currentPage().getParameters().get('Id');
        String  EmailCon =[Select email from Contact where Id=:conId ].email;
        sendmailexample testDObj1 = new sendmailexample(sc2);
        testDObj1.AddRecep='twilms@verisign.conm';
        testDObj1.to ='twilms@verisign.conm';
        testDObj1.EmailCon ='twilms@verisign.conm'; 
        testDObj1.Email ='twilms@verisign.conm';
        testDObj1.body='abc';
        testDObj1.subject='abc';
           
         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
         sendmailexample testDObj = new sendmailexample(sc1);
          testDObj.sendpost();        
           test.stopTest();  
            
    }   
    static testMethod void runTest5(){
  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; 
       
        ApexPages.StandardController sc2 = new ApexPages.StandardController(Contact);
        apexpages.currentpage().getparameters().put('ID',contact.id);
        String  conId = ApexPages.currentPage().getParameters().get('Id');
      String  EmailCon =[Select email from Contact where Id=:conId ].email;
      sendmailexample testDObj1 = new sendmailexample(sc2);
        testDObj1.AddRecep='twilms@verisign.conm';
        testDObj1.to ='twilms@verisign.conm';
        testDObj1.EmailCon ='twilms@verisign.conm'; 
        testDObj1.Email ='twilms@verisign.conm';
        testDObj1.body='abc';
         testDObj1.subject='abc';     
            
test.StartTest();
PageReference pageRef = page.sendmail;
Test.setCurrentPage(pageRef);
ApexPages.StandardController sc = new ApexPages.standardController(Contact);
sendmailexample testDObj = new sendmailexample(sc);
testDObj.EmailCon ='twilms@verisign.conm'; 
testDObj.processSelected(); 
test.StopTest();
}
  
}
 
Public  class sendmailexample{

    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 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 sendmailexample(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 sendmailexample(){
        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));
           }
        }
        
       
        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;
        }
    }   
}

 
Nagendra Prasad ChNagendra Prasad Ch
1) Solution for 1 st error,

You have to add to, EmailCon, Email.. etc for testDObj,  before calling testDObj.sendpost(). Modify your test method as follows.


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;
              
        ApexPages.StandardController sc2 = new ApexPages.StandardController(Contact);
        apexpages.currentpage().getparameters().put('ID',contact.id);
        String  conId = ApexPages.currentPage().getParameters().get('Id');
        String  EmailCon =[Select email from Contact where Id=:conId ].email;
        sendmailexample testDObj1 = new sendmailexample(sc2);
        testDObj1.AddRecep='twilms@verisign.conm';
        testDObj1.to ='twilms@verisign.conm';
        testDObj1.EmailCon ='twilms@verisign.conm'; 
        testDObj1.Email ='twilms@verisign.conm';
        testDObj1.body='abc';
        testDObj1.subject='abc';
           
         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
         sendmailexample testDObj = new sendmailexample(sc1);
         testDObj.AddRecep='twilms@verisign.conm';
        testDObj.to ='twilms@verisign.conm';
        testDObj.EmailCon ='twilms@verisign.conm'; 
        testDObj.Email ='twilms@verisign.conm';
        testDObj.body='abc';
        testDObj.subject='abc';

          testDObj.sendpost();        
           test.stopTest();  
            
    }




2) Second error solution,

You have to pass Id parameter just befor line no 67.
 apexpages.currentpage().getparameters().put('Id',contact.id); 

You entire test class should be(Contains modified code for both error1 and 2 ),

@isTest
public class sendcontacttest{  
    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;
              
        ApexPages.StandardController sc2 = new ApexPages.StandardController(Contact);
        apexpages.currentpage().getparameters().put('ID',contact.id);
        String  conId = ApexPages.currentPage().getParameters().get('Id');
        String  EmailCon =[Select email from Contact where Id=:conId ].email;
        sendmailexample testDObj1 = new sendmailexample(sc2);
        testDObj1.AddRecep='twilms@verisign.conm';
        testDObj1.to ='twilms@verisign.conm';
        testDObj1.EmailCon ='twilms@verisign.conm'; 
        testDObj1.Email ='twilms@verisign.conm';
        testDObj1.body='abc';
        testDObj1.subject='abc';
           
         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
         sendmailexample testDObj = new sendmailexample(sc1);
         testDObj.AddRecep='twilms@verisign.conm';
        testDObj.to ='twilms@verisign.conm';
        testDObj.EmailCon ='twilms@verisign.conm'; 
        testDObj.Email ='twilms@verisign.conm';
        testDObj.body='abc';
        testDObj.subject='abc';

          testDObj.sendpost();        
           test.stopTest();  
            
    }


    
    static testMethod void runTest5(){
  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; 
       
        ApexPages.StandardController sc2 = new ApexPages.StandardController(Contact);
        apexpages.currentpage().getparameters().put('ID',contact.id);
        String  conId = ApexPages.currentPage().getParameters().get('Id');
      String  EmailCon =[Select email from Contact where Id=:conId ].email;
      sendmailexample testDObj1 = new sendmailexample(sc2);
        testDObj1.AddRecep='twilms@verisign.conm';
        testDObj1.to ='twilms@verisign.conm';
        testDObj1.EmailCon ='twilms@verisign.conm'; 
        testDObj1.Email ='twilms@verisign.conm';
        testDObj1.body='abc';
         testDObj1.subject='abc';     
            
test.StartTest();
PageReference pageRef = page.sendmail;
Test.setCurrentPage(pageRef);
ApexPages.StandardController sc = new ApexPages.standardController(Contact);
apexpages.currentpage().getparameters().put('Id',contact.id);
sendmailexample testDObj = new sendmailexample(sc);
testDObj.EmailCon ='twilms@verisign.conm'; 
testDObj.processSelected(); 
test.StopTest();
}
  
}


 
rupesh ranjanrupesh ranjan
yes now this error

Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Stack TraceClass.sendmailexample.processSelected: line 138, column 1
Class.sendcontacttest.runTest5: line 74, column 1
Nagendra Prasad ChNagendra Prasad Ch

What is the code in Class  sendmailexample.processSelected: line 138, column 1 ?

Class might be referring some variable in that line, which you have to set in  or pass from test class.

rupesh ranjanrupesh ranjan
can you plz give me the email ID?
rupesh ranjanrupesh ranjan
This is the methd for selecting contact list. which stored in wrapper class 
u can check here (part of code)
 
Public  class sendmailexample{ 
public List<wrapContact> wrapContactList {get; set;}
    public List<Contact> selectedContacts{get;set;}
    Public String selectId {get;set;}
    public sendmailexample(){
        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));
           }
        }
        
       
        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;
        }
    }   
}

 
Nagendra Prasad ChNagendra Prasad Ch
Which line is 138 here?. Unable to find exact error line because this is a part of code.