Forums

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

behavior Get timespent values

Omprakash Thamsetty
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 22, 2019

How to get timespent value in behavior and set the value zero 0 if timespent is null.

I am getting below error with my code.

Here is my error

java.lang.NullPointerException: Cannot get property 'timeSpent' on null object

import org.apache.log4j.Category
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor

def issueId = formContents["id"]
def issue = ComponentAccessor.getIssueManager().getIssueObject(issueId as Long)

def TTSpent = issue.timeSpent?: "0"

def TTSpentVal = TTSpent as Long

//def TTSpent = issue.timeSpent as Long

if(TTSpentVal){ //Keep the Remaining Estimate from going into a negative number
TTSpent = 0;
}

def TimeSpent = TTSpentVal / 3600
//log.debug "time spent ${TimeSpent}"

def REst = totalEstimateDEVBA - TimeSpent;
//log.debug "Remaining est ${REst}"

def remainingEstimate = getFieldById("timetracking_remainingestimate")
def remainingEstimatestr = REst.toString() + "h"
remainingEstimate.setFormValue(remainingEstimatestr)

 

Does anyone has any idea to convert null to zero in code.

 

 

1 answer

0 votes
Mohamed Adel
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 24, 2019

@Omprakash Thamsetty 

Your problem is with the issue object itself , it return null. 

Omprakash Thamsetty
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 24, 2019

@Mohamed Adel yeah. its return null but how to set zero if it is null in my code ? This is in behavior

look like some thing below

TTSpentVal = issue.timespent

def TTSpentVal = 0

if(TTSpentVal){

     TTSpentVal1=issue.timespent as Long

}else{

 TTSpentVal1=0

}

 

but it is not checking value and setting zero. how to check value as null and set zero

Mohamed Adel
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 26, 2019

@Omprakash Thamsetty 

Try to update this line : 

def TTSpent = issue.timeSpent?: "0"

with:

def TTSpent = issue?.timeSpent?: "0"

Note : Your  return null object, in this case you are not checking the timeSpent value as the object itself is not exist therefore, it has no property.

Like Tim Perrault likes this

Suggest an answer

Log in or Sign up to answer