+ Start a Discussion
chanti kchanti k 

System.NullPointerException: Attempt to de-reference a null object Class.EmailMessage.<init>: line 16, column 1

Hi ,

Below is my code for the apex class.Could you please help me .
public class EmailMessage {

    public String emailTo {get;set;}
    public String emailCc {get;set;}
    public String subject {get;set;}
    public String body {get;set;}
    
    private final Lead lead;

    public PageReference cancel() {
        return null;
    }

     public EmailMessage() {
     list <lead> l= [select Description, ID from Lead where id = :ApexPages.currentPage().getParameters().get('id')];
       subject = lead.Description;
          } 
   
    public void sendEmail() {
        
        //create a mail object to send email 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        //string to hold To Addresses
        if (emailTo != '') {
            String[] toaddress = (new String[]{emailTo});
            //set email properties 
            mail.setSubject(subject);
            mail.setUseSignature(true);
            mail.setPlainTextBody(body); 
            mail.setTargetObjectID(lead.id);
            mail.setToAddresses(toaddress);

         if (emailCc != '') {
            String[] ccaddress = (new String[] {emailCc});
            mail.setCcAddresses(ccaddress);
            }
    
            // send the email
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});  
            
            lead.status = 'Email Sent';
            Update lead;
        }           
     }   
 }
Chanti,

 
Manohar kumarManohar kumar

Hi Chanti, may be in line 16 lead is null.put system.debug('lead:'+lead); and what the value is coming.

i am not sure why you are doing subject = lead.Description;

i think you should put...

if(l.size()>)) {

 subject = l[0].Description;
 lead = l[0];

}


pls let me know how it worked out.

Thanks,

Manohar

Amit Chaudhary 8Amit Chaudhary 8
update your code like below
public class EmailMessage {

    public String emailTo {get;set;}
    public String emailCc {get;set;}
    public String subject {get;set;}
    public String body {get;set;}
    
    private final Lead lead;

    public PageReference cancel() {
        return null;
    }

    public EmailMessage() 
	{
      list <lead> l= [select Description, ID from Lead where id = :ApexPages.currentPage().getParameters().get('id')];
	 
	  if(l.size() > 0 )
	  {
	    lead =l[0];
		subject = lead.Description;
	  }	
    } 
   
    public void sendEmail() {
        
        //create a mail object to send email 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        //string to hold To Addresses
        if (emailTo != '') {
            String[] toaddress = (new String[]{emailTo});
            //set email properties 
            mail.setSubject(subject);
            mail.setUseSignature(true);
            mail.setPlainTextBody(body); 
            mail.setTargetObjectID(lead.id);
            mail.setToAddresses(toaddress);

         if (emailCc != '') {
            String[] ccaddress = (new String[] {emailCc});
            mail.setCcAddresses(ccaddress);
            }
    
            // send the email
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});  
            
            lead.status = 'Email Sent';
            Update lead;
        }           
     }   
 }

 
SandhyaSandhya (Salesforce Developers) 
Hi Chanti,

Please see below code.
 
public class EmailMessage1 {

    public String emailTo {get;set;}
    public String emailCc {get;set;}
    public String subject {get;set;}
    public String body {get;set;}
    
    private final Lead ld {get;set;}

    public PageReference cancel() {
        return null;
    }

     public EmailMessage1() {
     Lead ld= new Lead();
      ld= [select Description, ID from Lead where id = :ApexPages.currentPage().getParameters().get('id')];
     
          } 
   
    public void sendEmail() {
        
        //create a mail object to send email 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        //string to hold To Addresses
        if (emailTo != '') {
            String[] toaddress = (new String[]{emailTo});
            //set email properties 
            mail.setSubject(ld.Description);
            mail.setUseSignature(true);
            mail.setPlainTextBody(body); 
            mail.setTargetObjectID(ld.id);
            mail.setToAddresses(toaddress);

         if (emailCc != '') {
            String[] ccaddress = (new String[] {emailCc});
            mail.setCcAddresses(ccaddress);
            }
    
            // send the email
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});  
            
            ld.status = 'Email Sent';
            Update ld;
        }           
     }   
 }

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
chanti kchanti k
Thanks allot Amit ,your Answer is perfectly working :)