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
cmoynihancmoynihan 

Attempt to de-reference a null object

I am getting an error with the line "mail.setToAddresses" in the following code:

 

String email = '';
String ownerName = '';
// Check if Owner is a User if (ownerType == 'User'){ User owner = [select Name, Email from User where Id = :c.OwnerId]; email = owner.Email; ownerName = owner.Name; System.debug('Email: ' + email + ', Name: ' + ownerName); String[] toAddresses = new String[]{email}; mail.setToAddresses(toAddresses); ...

 The debug message is outputting the Email (and Name) just fine, so I am not sure what the problem is. I use the same syntax is another method and it works just fine, so I'm really not sure what the issue is. Any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Please provide your complete code as I can suspect "mail." is having some problem. Make sure you are "using Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();". Means you are instantiating the "Messaging.SingleEmailMessage".

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

Aldo_FAldo_F

Instead of:

 String[] toAddresses = new String[]{email};

 

Try to use:

List<String> toAddresses = new List<String>{email};

 

Also, make SURE email is not null

 

Regards,

--Aldo

Ankit AroraAnkit Arora

Please provide your complete code as I can suspect "mail." is having some problem. Make sure you are "using Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();". Means you are instantiating the "Messaging.SingleEmailMessage".

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
cmoynihancmoynihan

Like a fool, my mail declaration was somehow after my mail.setToAddress call. Oops. It was fine in my other methods.

 

Sorry for the idiocy!

Shashikant SharmaShashikant Sharma

You must not have initialized this instance of Messaging.SingleEmailMessage mail

So just initialize it before you refer it

 

like

mail = new Messaging.SingleEmailMessage();

 

 

Shashikant SharmaShashikant Sharma

cross post

Ankit AroraAnkit Arora

It happens many time with us also cmoynihan. You are not the single one in the race :-)

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page