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

AndreH August 4, 2014

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
Answer accepted
JamieA
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.
August 4, 2014

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 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2014

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.

AndreH August 4, 2014

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2014

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.


AndreH August 4, 2014

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;

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2014

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!

AndreH August 4, 2014

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 4, 2014

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.

AndreH August 4, 2014

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