Forums

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

Script is not working, throwing an error

neeta jain January 20, 2021

Hi Team,

I have implemented a script for calculating Number Of Weeks:

import com.atlassian.jira.component.ComponentAccessor

 

def dateFieldObject = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Start Date")[0]

def dateFieldObject2 = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("End Date")[0]

 

def dateValue= issue.getCustomFieldValue(dateFieldObject) as Date

def dateValue2= issue.getCustomFieldValue(dateFieldObject2) as Date

def now = new Date()

 

def diffdate

 

// if "End Date" is reached or exceeded, which means "now" is later than "End Date"

if (now.after(dateValue2)){

    // End Date - Start Date

    diffdate = dateValue2 - dateValue

} else {  // if now is before or equal to "End Date"

    // End Date - now

                diffdate = dateValue2 - now

}

 

if (diffdate) {

    double noofweeks = diffdate/7

    double roundNoOfWeeks = Math.round(noofweeks)

    int value = (int) roundNoOfWeeks;

                return value

}

 

but getting attached error, please kindly check and give me some solution where I am doing error.PNGwrong.

 

2 answers

0 votes
Nic Brough -Adaptavist-
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.
January 20, 2021

The error message says line 6 is failing.  I think the code on that line is the one trying to get a date from the "start date" field.  A null pointer error here means that there is no data in something you are passing into a method on that line.

Either the field does not exist for this issue, or the data in it is not a date, or is empty.

0 votes
Mohamed Benziane
Community Champion
January 20, 2021

Hi @neeta jain 

 

Take a look at this documentation, you will find some good stuff to begin wih

https://www.adaptavist.com/blog/effortlessly-calculate-dates-and-times-with-the-adaptavist-library-in-jiranew-blog-post

hope this helps

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events