Forums

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

ScriptRunner - "Resolution time" scripted field. Cannot invoke method getTime() on null object

Egle K. March 30, 2018

Hello,

I'm quite new in Scriptrunner, but was trying to script custom field, which shows the duration from issue creation to status 'resolved'.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def createDate = issue.getCreated().getTime();
def resolutionDate = issue.getResolutionDate().getTime();
if (resolutionDate == null) {
return null;
}
return (resolutionDate - createDate)/1000 as long;

(Searcher: Duration Searcher
Template: Duration)

 

Actually the output is correct (see attachment):

output.JPG

but logs sometimes show:

2018-03-30 10:19:57,121 ERROR [customfield.GroovyCustomField]: **********
java.lang.NullPointerException: Cannot invoke method getTime() on null object
	at Script952.run(Script952.groovy:7) 

It's obvious that this error appears only if you're working in open (not resolved) issues.

How the logic should be modified so that the field only "work for" resolved issues? 

1 answer

1 accepted

2 votes
Answer accepted
Alexey Matveev
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.
March 30, 2018

You should write like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issue = ComponentAccessor.getIssueManager().getIssueObject("MAV-2")

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def createDate = issue.getCreated().getTime();
def resolutionDate = issue.getResolutionDate()?.getTime();
if (resolutionDate == null) {
return null;
}
return (resolutionDate - createDate)/1000 as long;
Bhanu
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.
April 11, 2019

This calculates for all the time between created and resolved dates. Is there a way to exclude the times from 5PM to 9AM everyday and also the weekends?

Dennis Ebner July 2, 2023

@Bhanuhave you found a solution? 

Suggest an answer

Log in or Sign up to answer