---
title: "Tutorial on safe access to a field"
canonical: "https://support.appfire.com/space/JMWE/461964371/Tutorial%20on%20safe%20access%20to%20a%20field"
format: markdown
---
> Macro (aura-html)

> Macro (excerpt)
> 
> <span style="color: #333333">When you access the field of an issue, </span><span style="color: #222222">you might need to verify that it is not</span><span style="color: #222222"> </span>`null`<span style="color: #222222"> before calling a method on the value of the field. </span>This tutorial will guide you through writing Groovy scripts that access the fields of an issue <span style="color: #333333">avoiding exceptions due to a </span>`null`<span style="color: #333333"> value. </span>


**<span style="color: #333333">On this page:</span>**


<span style="color: #262626">Imagine you want to access the </span>`username`<span style="color: #262626"> of the user, the issue is assigned to. </span><span style="color: #262626">Let us write a script for the same in the Groovy script tester of the JMWE Administration page.</span>

### Step 1 - Navigate to the Groovy script tester in the JMWE administration pages

1. Go to the **Administration** icon > Macro (inline-media-image)

 and click on it.
2. Locate **Add-ons** from the menu and click on it.
3. Locate **JIRA MISC WORKFLOW EXTENSIONS** on the left panel.
4. Click on [Groovy script tester](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/78056956).

### Step 2 - Write the script in the editor

1. <span style="color: #333333">Write the following script in the editor.</span>

### Step 3 - <span style="color: #333333">Test your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test Groovy Script`<span style="color: #333333">.</span>
2. <span style="color: #333333">Input the issue key</span><span style="color: #333333"> </span>`GIJ-1`
3. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test`
4. <span style="color: #333333">The following result will be displayed.</span>

## 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.

### Step 1 - Fix<span style="color: #333333"> your script using if structure</span>

1. <span style="color: #333333">Click on the editor.</span>
2. <span style="color: #333333">Replace the script with the following script</span>

### Step 2 - <span style="color: #333333">Retest your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test again`<span style="color: #333333">.</span>
2. <span style="color: #333333">The following result will be displayed. </span>

## Avoid null pointer exception using the Safe navigation operator

You can simplify your script using the Safe navigation operator **?** and avoid <span style="color: #262626">null pointer exceptions. Let us use it in the script and test it.</span>

### Step 1 - Simplify<span style="color: #333333"> your script using the Safe navigation operator</span>

1. <span style="color: #333333">Click on the editor.</span>
2. <span style="color: #333333">Replace the script with the following script</span>

### Step 2 - <span style="color: #333333">Retest your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test again`<span style="color: #333333">.</span>
2. <span style="color: #333333">The following result will be displayed. </span>


## Avoid java.util.NoSuchElementException using the if structure

There might be cases where your script returns an empty collection and you will receive a `java.util.NoSuchElementException` if you call a method on the empty collection. You can avoid this using the if control structure *only*. For example, if you want to access the last comment on an issue and there are no comments for the issue you will face a `java.util.NoSuchElementException`. 

### Step 1 - Write the script in the editor

1. <span style="color: #333333">Write the following script in the editor.</span>

### Step 2 - <span style="color: #333333">Test your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test again`<span style="color: #333333">.</span>
2. <span style="color: #333333">The following result will be displayed.</span>
3. > Macro (inline-media-image)


### Step 3 - Try the<span style="color: #333333"> Safe navigation operator</span>

1. <span style="color: #333333">Click on the editor.</span>
2. <span style="color: #333333">Replace the script with the following script</span>

### Step 4 - <span style="color: #333333">Retest your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test again`<span style="color: #333333">.</span>
2. <span style="color: #333333">The same result will be displayed because u</span>sing the Safe navigation operator does not fix the issue.
3. > Macro (inline-media-image)

### Step 5 - Fix<span style="color: #333333"> your script using the if structure</span>

1. <span style="color: #333333">Click on the editor.</span>
2. <span style="color: #333333">Replace the script with the following script</span>

### Step 6 - <span style="color: #333333">Retest your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test again`<span style="color: #333333">.</span>
2. <span style="color: #333333">The following result will be displayed</span>

## <span style="color: #333333">Safe navigation operator in a chained expression</span>

<span style="color: #333333">Let us now write a script using the Safe navigation operator in an example that has </span><span style="color: #262626">a complicated chained expression. Imagine you want to access the Release date of the first Affects Version/s.</span>

### Step 1 - Write the script in the editor

1. <span style="color: #333333">Write the following script in the editor.</span>

### Step 2 - <span style="color: #333333">Test your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test again`<span style="color: #333333">.</span>
2. <span style="color: #333333">The following result will be displayed.</span>
3. > Macro (inline-media-image)


### Step 3 - Apply the <span style="color: #333333">Safe navigation operator to the first() method</span>

1. <span style="color: #333333">Click on the editor.</span>
2. <span style="color: #333333">Replace the script with the following script</span>

### Step 4 - <span style="color: #333333">Retest your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test again`<span style="color: #333333">.</span>
2. <span style="color: #333333">The following result will be displayed because you cannot get </span>`releaseDate`<span style="color: #333333"> on a null object</span>
3. > Macro (inline-media-image)

### Step 5 - Apply the safe navigation operator to the releaseDate

1. <span style="color: #333333">Click on the editor.</span>
2. <span style="color: #333333">Replace the script with the following script</span>

### Step 6 - <span style="color: #333333">Retest your script</span>

1. <span style="color: #333333">Click on</span><span style="color: #333333"> </span>`Test again`<span style="color: #333333">.</span>
2. <span style="color: #333333">The following result will be displayed</span>

### Next >> [Looping over collections](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/462062361)