1

I have the following component class, whose function is solely to modify a string for an email template. It works fine, inserting the concatenated string into the email.

public class ListOfMissingItems {
    public String missingItems{get;set;}
    private String formattedMissingItemsList;

    public ListOfMissingItems() {
        formattedMissingItemsList = this.getListOfMissingItems();
    }

    public String getListOfMissingItems() {
        return missingItems + ' BLORFF'; //.replaceAll('Som', '###');
    }
}

However, if I drop in replaceAll (as seen in the comment) in place of the concatenation, I get this error:

Error occurred trying to load the template for preview: Attempt to de-reference a null object. Please try editing your markup to correct the problem.

It's almost as if the value isn't present when replaceAll is called, but it is when the concat operation is in place. What's going on?

2

Your diagnosis is likely correct. To avoid the problem include a null guard:

return missingItems != null ? missingItems.replaceAll('Som', '###') : null;

The getListOfMissingItems method may be called more than once, and this will stop an earlier problem when missingItems is null from interfering with the later case when missingItems is not null.

1

Your property missingItems is not initialized (and is thus null). You can verify this for yourself by making an instance of your controller in an anoymous apex window (through the developer console, typically) and then writing system.debug(myController.missingItems);

If you try to call getListOfMissingItems() prior to putting a value into missingItems, you'll get an NPE (null. = NPE).

String concatenation appears to be one of the few things that can handle a null argument. If you were to print missingItems to a log when doing the concatenation, you'd likely see "null BLORFF" as the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.