0

Fellow developers,

I have a call to an email validation function in my before insert in my trigger, which basically puts the email blank if its not valid (according to the regex)

public static String validEmailOrBlank(String email)
{
    String emailRegex = '([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})';
    Pattern emailPattern = Pattern.compile(emailRegex);

    Matcher emailMatcher = emailPattern.matcher(email);

    if (!emailMatcher.matches()) {
        email = '';
    }
    return email;
}

This works fine if I manually create a record (in this case it is a custom object) which empties the email field (which is just a regular text fieldtype, no email field type, this is intentional) However, when I use the dataloader to mass import records, I get nullpointers

... execution of BeforeInsert
caused by: System.NullPointerException: Script-thrown exception
(System Code)
Class.Utilities.validEmailOrBlank: line 9, column 1
...

line 9 in this case is:

Matcher emailMatcher = emailPattern.matcher(email);

Anyone have an idea?

1
  • Not too novel approach but, can you try wrapping this in a try catch block try{ Matcher emailMatcher = emailPattern.matcher(email); } catch(exception ex){ } (or) if(email.lenght()> 0 && email != ''){ Matcher emailMatcher = emailPattern.matcher(email); }
    – Rao
    Oct 26, 2012 at 17:04

1 Answer 1

1

The csv file you're loading potentially contains null email addresses. (I was able to reproduce the NullPointerException by passing a null string to your method in Execute Anonymous, so a fair assumption I would think)

Your code, as it stands, would throw a null pointer exception if the email field is null. So add a not null check for email before attempting to compile a pattern match

public static String validEmailOrBlank(String email)
{
if (email != null) { //NOT NULL CHECK
    String emailRegex = '([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})';
    Pattern emailPattern = Pattern.compile(emailRegex);

    Matcher emailMatcher = emailPattern.matcher(email);

    if (!emailMatcher.matches()) {
        email = '';
    }
   }
    return email;
}

Just tried this and it works alright.

5
  • @techtrkker email seems to be a string param !!!! Would it not be more accurate to check for the email != '' before it enters the line Matcher emailMatcher = emailPattern.matcher(email);
    – Rao
    Oct 26, 2012 at 17:10
  • It doesn't really matter, if it is null you don't have to do much. I would even consider adding a not null check before invoking the method. Oct 26, 2012 at 17:38
  • Also an empty string is not the same as a null string. An empty string i.e '' wouldn't cause a NullPointerException, only a null string does. Oct 26, 2012 at 17:46
  • I feel such a tool, such a simple solution, however it was a combination of wrong mapping in the CSV (because all the emails were filled in), a long day at work, tunnelvision on the problem, and now its fixed, thanks for the help :)
    – pjcarly
    Oct 26, 2012 at 18:20
  • Glad it helped. We can all do with a fresh pair of eyes on a long day! :) Oct 26, 2012 at 18:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .