The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

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

Help with counting how many components on a Jira issue

Inayat Nahvi
Contributor
August 14, 2020

I'm working with Jira 8.5 and the latest Jira Misc Custom Fields plugin release.  So I created a custom calculated field with the following Groovy script:

// Get Component Count
def compcount = issue.get("components").size() as Integer

// Check if Component Count is valid
if (compcount != null){
return compcount
}else{
// return zero to indicate a null value in one of the fields
return "0"
}

This works correctly when an issue has at least one component.  But if no component is set, it spits out the error below instead of a 0.   I would I guess I am missing something obvious, but I not very familiar with Groovy scripting.

Message:
java.lang.NullPointerException: Cannot invoke method size() on null object
Stack:
org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:43)
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:34)
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:119)
script_ee204201d3efffe787713c4b71f3d589.run(script_ee204201d3efffe787713c4b71f3d589.groovy:2)

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
David Fischer
Community Champion
August 14, 2020

Try this formula instead:

issue.get("components")?.size() ?: 0
Inayat Nahvi
Contributor
August 14, 2020

@David Fischer , yes, that fixed it.  Thanks for the lightning quick response!

Teja
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
October 7, 2020

@David Fischer 

How can I solve this 

def issueManager = ComponentAccessor.getIssueManager()

//Get the VE
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Validating Environments")

//Get the VE value from Database
def sizeVE = issueManager.getIssueObject(issue.id).getCustomFieldValue(cf) as List

def value1 = sizeVE.size() > 1 //Line:21

the value1 returning null instead of emptylist, when my value is empty on database

java.lang.NullPointerException: Cannot invoke method size() on null object
 at Script1678.run(Script1678.groovy:21)
David Fischer
Community Champion
October 8, 2020

Hi @Teja ,

you don't need all this complicated code. This will do:

issue.get("Validating Environments")?.size() > 1

The question mark will avoid the NPE you were seeing. 

Like Teja likes this
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events