Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner Error: Cannot invoke method getCustomFieldValue() on null object

Brian
Contributor
June 21, 2020 edited

Hello ScriptRunner enthusiasts,

We've developed a ScriptRunner scripted field called "In Epic" that is supposed to return the Epic Name of a Story or Sub-Task.

For example if Epic ABC-123 had a Story ABC-456 the 'In Epic" field would return in a string format the Epic Name (a field in Jira).  Our users like to remember Epic names, not Epic keys.

In our log file we get this error.

 

2020-06-21 20:10:34,777+0000 IssueIndexer:thread-16 ERROR ub57988 1186x56429x2 1tazjek 204.138.240.254,10.12.0.237 /secure/admin/IndexReIndex!reindex.jspa [c.o.scriptrunner.customfield.GroovyCustomField] *************************************************************************************
2020-06-21 20:10:34,779+0000 IssueIndexer:thread-16 ERROR ub57988 1186x56429x2 1tazjek 204.138.240.254,10.12.0.237 /secure/admin/IndexReIndex!reindex.jspa [c.o.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: CBTGRV-1371, field: In Epic
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
   at Script186.run(Script186.groovy:36)

 

-----------------------------------------------------------------------------------------------------

Here is the code itself

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue

def issueManager = ComponentAccessor.getIssueManager() //Get Issue Manager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() //Get Custom Field Manager

def cfIssueType = issue.issueType.getName() //Get issue type name in string format
def cfIssueKey = issue.key //Get issue key in string format
def epicLink = customFieldManager.getCustomFieldObjectsByName('Epic Link')[0] //Get Epic Link field object
def epicName = customFieldManager.getCustomFieldObjectsByName('Epic Name')[0] //Get Epic Name field object

log.warn('Issue type: ' + cfIssueType)

switch(cfIssueType) {

case ['Story']:

def epicKey = issue?.getCustomFieldValue(epicLink).toString() //Get Epic link field value
if (epicKey) {
def epicIO = issueManager.getIssueObject(epicKey) //Return issue object from EpicLink Key
def epicDisplayName = epicIO.getCustomFieldValue(epicName) //Return Epic Display Name as string
return epicDisplayName
}
break;

case ['Sub-task']:

def parentStory = issue.parentObject //Define parentStory as issue paren object
def epicKey = parentStory?.getCustomFieldValue(epicLink).toString() //Get Key value of Epic Link as string
if (epicKey) {
def epicIO = issueManager.getIssueObject(epicKey) //Return issue object from EpicLink Key
def epicDisplayName = epicIO.getCustomFieldValue(epicName) //Return Epic Display Name as string
return epicDisplayName
}
break;

default:
return
break;
}

 

 

We want to figure out how to deal with this null object error that the log file is reporting.

sample asd code block

 

1 answer

0 votes
Marc-André Tremblay June 22, 2020

The error is because the issueManager is unable to find the requested key so epicIO is null. Trying to getcustomFieldValue() from a null issue throws this error.

Either you null check with a "?" afterepcioIO:

def epicDisplayName = epicIO?.getCustomFieldValue(epicName) //Return Epic Display Name as string

but  this may return a null value  to th eepic name.

You can also add an elvis operator to return an empty value:

def epicDisplayName = epicIO?.getCustomFieldValue(epicName)  ?: ""

This will ensure an empty string if the is no found value.

I dont use Jira Software much but you may want to print a log of the epicKey value...I think it may not return waht you think it is otherwise issueManager would find the issue easily.

Uday Kiran Raparthy September 10, 2020

Hi @Marc-André Tremblay 

i am trying to copy custom field value from parent to all child issues and using the below code 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def issue = event.issue
def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_13202')
def parentMyFieldValue = issue.parentObject.getCustomFieldValue(field)
def changeHolder = new DefaultIssueChangeHolder();
field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field), parentMyFieldValue),changeHolder);

 

getting same error as above

Log:

2020-09-10 16:59:14,964 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-09-10 16:59:14,965 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
 at Script320.run(Script320.groovy:11)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.3
TAGS
atlassian, jira service management, jsm webinar, ai in jsm, opsgenie integration, incident management, virtual agent, atlassian intelligence, ai-powered service desk, it operations, atlassian learning, service management webinar, team '25 recap

What’s new in Jira Service Management 🤔

Discover how Atlassian is revolutionizing service management with cutting-edge solutions for AI-powered support, HR Service Management, or DevOps connectivity.

Register here ⬇️
AUG Leaders

Atlassian Community Events