Scripted Field generates Cannot invoke method getCustomFieldValue() on null object

Hello,

I have 2 separate Scripted Fields on a Sub-task. These fields are reading existing data from a custom field on the parent issue.

Field 1 on Sub-task (Parent Prod Start Date). This field reads a custom date field from the parent issue and displays it on the sub-task.

Field 2 on Sub-Tasks (Parent DBA Estimate). This field reads a custom numeric field from the parent issue and displays it on the sub-task.

Both of these scripted fields is generating errors in the logs during a re-indexing and also when a user is working on a parent issue.

error - javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object. A stacktrace has been logged.

--------------------------------------

Here is the code for Parent Prod Start Date -

import com.atlassian.jira.issue.Issue;

import com.atlassian.jira.ComponentManager;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.component.ComponentAccessor;

def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Prod Install Start Date/Time")

def abc456 = issue.parentObject.getCustomFieldValue(field);

return abc456;

Here is the code for Parent DBA Estimate -

import com.atlassian.jira.issue.Issue;

import com.atlassian.jira.ComponentManager;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.component.ComponentAccessor;

def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("DBA Estimate")

def abc123 = issue.parentObject.getCustomFieldValue(field);

return abc123;

--------------------------------------------

Here is the complete error -

2014-08-05 08:45:32,448 ajp-bio-8009-exec-17 ERROR ahughes 525x6851x1 vihicd 1.1.1.1 /plugins/servlet/com.onresolve.ScriptedFieldPreviewServlet [onresolve.jira.groovy.GroovyCustomField] javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object

2 answers

1 accepted

1 vote

Nic is correct as usual, but I don't think that's the problem.

I did a quick test and it seems like jira requests the value of the custom field even when it's not in the context. I don't know why... I think there is a reported bug for this in jamieechlin.atlassian.net/browse/GRV but it's not fixed, because I'm not sure what I can do. I need to reproduce and report to Atlassian.

In the meantime I would do:

if (issue.isSubTask()) {
...
return something
}
return null

0 vote

This can happen when either

  1. The field you are trying to read has no data
  2. The field you are trying to read is not valid for that project/issuetype

In both cases, wrapping the "getCustomFieldObject" call in "if field not null" type if blocks should fix it.

Hi Nic,

I am not able to get the logic correct for checking against the "if field not null". Do you have an example of the syntax?

Thanks

Argh, sorry, I've made a mistake

Most of your code is actually ok, and, for the record, this is what I do to avoid empty field errors:

CustomField budgetField = customFieldManager.getCustomFieldObjectByName(budgetFieldName);

def thisPaidCR = issue.getCustomFieldValue(paidDaysField)
  if ( thisPaidCR )
    { 
    total += thisPaidCR
    }

But although that will catch the error I thought you're getting, I have looked in the wrong place.

You have:

issue.parentObject.getCustomFieldValue(field);

The problem is not "getCustomFieldValue", it's "parentObject". That is returning a null.


Yes, it is on the parent object where the null error is coming.

i tried to verify there is a valid field on the parent, however it is still generating the same issue.

Here is the new code, that still doesn't work

def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Prod Install Start Date/Time")

def abc456 = issue.parentObject.getCustomFieldValue(field);

def total = "Hello";

if (abc456 != null)

{

total = (abc456)

}

return total;

Ok, I have a thought - could you try

def abc456 = issue.parentObject().getCustomFieldValue(field);

I think parentObject is a method, not a field, so we need ()

p.s. I still apologise for looking in completely the wrong place at first, I feel like I owe you a pint or something to make up for wasting your time!

I tried -

import com.atlassian.jira.issue.Issue;

import com.atlassian.jira.ComponentManager;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.component.ComponentAccessor;

field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Prod Install Start Date/Time")

def abc456 = issue.getParentObject().getCustomFieldValue(field);

if (issue.isSubTask())

{

return abc456

}

return null;

however it is still not working for the parent issue

I'm not sure it can work for the parent issue - by definition, they won't have a parent themselves, and, if I have understood your config, they won't have the field either. I'd move the "def abc456" inside the "if (issue.isSubTask())" block, then it won't try to retreive the parent or the field when there's nothing there.

Thank you!

moving that in seems to have taken care of the error -

import com.atlassian.jira.issue.Issue;

import com.atlassian.jira.ComponentManager;

import com.atlassian.jira.issue.CustomFieldManager;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.component.ComponentAccessor;

field = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_11401')

if (issue.isSubTask())

{

def abc456 = issue.getParentObject().getCustomFieldValue(field);

return abc456

}

return null;

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

159 views 1 17
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you