+ Start a Discussion
rupesh ranjanrupesh ranjan 

Any one can remove this error from test class

Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Stack TraceClass.sendmailexample.processSelected: line 26, column 1
Class.sendmailexample1test.runTest1: line 37, column 1
 
@isTest
public class sendmailexample1test{  
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;
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();
}
  
}
 
Public  class sendmailexample1{ 
    public String to{get; set;}
    public String AddRecep{get; set;}
    public String subject {get; set;}
    public String body {get; set;}
public List<wrapContact> wrapContactList {get; set;}
    public List<Contact> selectedContacts{get;set;}
    Public String selectId {get;set;}
    public sendmailexample1(){
        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 = '';         
      
    }         
    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;
        }
    }   
}

 
Mahesh DMahesh D
Hi Rupesh,

Please find the below Test Class:
@isTest
public class sendmailexample1test{  
    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.com';
        contact.MobilePhone = '918527116723';
        insert contact; 
       
        sendmailexample1 testDObj1 = new sendmailexample1(sc2);
        testDObj1.AddRecep='twilms@verisign.com';
        testDObj1.to ='twilms@verisign.conm';
        testDObj1.EmailCon ='twilms@verisign.conm'; 
        testDObj1.Email ='twilms@verisign.conm';
        testDObj1.body='abc';
        testDObj1.subject='abc'; 
        
        for(sendmailexample1.wrapContact wrapContactObj : testDObj1.wrapContactList) {
            wrapContactObj.selected = true;
        }
        testDObj1.processSelected();
        System.assertEquals(testDObj1.AddRecep, 'twilms@verisign.com;twilms@verisign.com');
    }  
}

You have to check the assertions properly.

Please do let me know if it helps you.

Regards,
Mahesh
Mahesh DMahesh D
Please take the below code as I changed one line:
 
@isTest
public class sendmailexample1test{ 
	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.com';
		contact.MobilePhone = '918527116723';
		insert contact;
	   
		sendmailexample1 testDObj1 = new sendmailexample1();
		testDObj1.AddRecep='twilms@verisign.com';
		testDObj1.to ='twilms@verisign.conm';
		testDObj1.EmailCon ='twilms@verisign.conm';
		testDObj1.Email ='twilms@verisign.conm';
		testDObj1.body='abc';
		testDObj1.subject='abc';
	   
		for(sendmailexample1.wrapContact wrapContactObj : testDObj1.wrapContactList) {
			wrapContactObj.selected = true;
		}
		testDObj1.processSelected();
		System.assertEquals(testDObj1.AddRecep, 'twilms@verisign.com;twilms@verisign.com');
	} 
}

Regards,
Mahesh