Forums

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

script for Counting how many times a bug was reopened throws null pointer exception

Vidhya Mohan
Contributor
June 7, 2021

Hello,

I've a script placed in 'post function' of 'reopen' transition to increase the counter of 'Reopen Count' field. It works sometimes but other times it throws null pointer exception 

Below is the script

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def reopenCountCf = customFieldManager.getCustomFieldObject(18906)
def reopenCount = issue.getCustomFieldValue(reopenCountCf) as Double

reopenCount++

def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issue.setCustomFieldValue(reopenCountCf, reopenCount)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

The error that I get is 

2021-06-07 14:12:06,115 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue E2ET-45536 for user 'xxx'. View here: https://jiraprd-proximuscorp.msappproxy.net/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=0_e2e_bug_wf&descriptorTab=postfunctions&workflowTransition=331&highlight=1
java.lang.NullPointerException: Cannot invoke method next() on null object
at Script26.run(Script26.groovy:10)

 

What is wrong with my code? 

1 answer

1 accepted

0 votes
Answer accepted
Ravi Sagar _Sparxsys_
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 Leaders.
June 7, 2021

Hi @Vidhya Mohan 

Does it fail when there is no value in the custom field? If yes may be you can try to add a check if the custom field value is null (empty) then instead of doing reopenCount++ just try to update the field value with 1.

I hope it works.

Ravi

Vidhya Mohan
Contributor
June 7, 2021

Hello @Ravi Sagar _Sparxsys_ ,

 

You are right.. When the value is null it is failing. If the value is 0 or something else, then it is working correct. I'm new to groovy scripting. The script above I got it from atlassian forum. Would it be possible for you to help me with the changes that needs to be made to the script?

Regards,

Vidhya

Suggest an answer

Log in or Sign up to answer