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 objectStack:
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)
Try this formula instead:
issue.get("components")?.size() ?: 0
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.