0

I am trying to fetch all users of a role and add their email into a string[] i have some syntax.

Error:"common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object"|0x2f253344

My Code.

    String[] s;
    for (User a : [Select id,name,email from User where UserRoleId = '00E90000000dffgEAA']) {
        s.add(a.email);
    }
Shanker Paudel
  • 740
  • 1
  • 9
  • 21

2 Answers2

0

I think, it is giving error, because your soql query is not returning any rows. You can do something like this to get rid of errors...

Integer number_of_rows =  [Select id,name,email from User where UserRoleId = '00E90000000dffgEAA'];
if(number_of_rows>0)
{
    String[] s;
    for (User a : [Select id,name,email from User where UserRoleId = '00E90000000dffgEAA']) {
        s.add(a.email);
    }
}
0

The list of strings should be initialized by an empty list. It was easy.

String[] s = new String[] {};
hynekcer
  • 14,942
  • 6
  • 61
  • 99