Protect Groovy formulas against null values

When you access a field of an issue, you might need to verify that it is not null before calling a method on the value of that field to avoid null pointer exceptions. This article explains some approaches to protect your Groovy formulas written in the JMWE Groovy editor against null values. 

Approaches

  1. Avoid exceptions using the if structure

    You can explicitly check for null using the if control structure and then call a method on the field value. For example:

    if(issue.get("assignee")){
      issue.get("assignee").name
    }
  2. Using the Safe navigation operator

    You can simplify your script using the Safe navigation operator ?. and avoid null pointer exceptions. For example:

    issue.get("assignee")?.name