Getting null pointer exception while setting a custom field in groovy script

Hi,

I am trying to write this simple script that sets the value of a custom field depending on a value of a different custom field

This is the code

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.customfields.CustomFieldType

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

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.project.Project

import com.atlassian.jira.project.ProjectManager

import com.atlassian.jira.issue.issuetype.IssueType

import com.atlassian.jira.issue.IssueImpl





IssueManager issueManager = ComponentManager.getInstance().getIssueManager();

CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager();

ProjectManager projectManager = ComponentManager.getInstance().getProjectManager();



MutableIssue myissue = issueManager.getIssueObject("POA-6");



CustomField cfAPType = cfManager.getCustomFieldObjectByName("AP type");

CustomField cfPOType = cfManager.getCustomFieldObjectByName("PO type");

CustomField cfProposedDeveloper = cfManager.getCustomFieldObjectByName("Proposed Developer");





def currentissuetype=myissue.getIssueType()

def currentissuetypenm=currentissuetype.name

def currentAPType=myissue.getCustomFieldValue(cfAPType)

def currentPOType=myissue.getCustomFieldValue(cfPOType)

def currentdev=myissue.getCustomFieldValue(cfProposedDeveloper)

def developer="pink blue"



if (currentissuetypenm == "Zaratec")

     {

     myissue.setCustomFieldValue(currentdev,developer)

     }

Any idea why I am getting a null pointer exception.

javax.script.ScriptException: java.lang.NullPointerException

Thanks

2 answers

Hi

How do you try to run that script?

And at which line do you get the NullPointerException?

Greetings

Hermann

Hi Shamhoon,

if i got it, you want to set the value of custom field according the issue type. If that's right, code should be something like:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;

  
// gets a reference to the IssueManager and CustomFieldManager manager classes
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();


//Get the custom field you want to update by id

CustomField customField_toupdate = customFieldManager.getCustomFieldObject( cf_id );




//issue type condition


if (issue.getIssueTypeObject().getName() == "issue_type_name"){

def val_test = "enter_here_the_desired_value"

issue.setCustomFieldValue(customField_toupdate, val_test)
}

Test it first.

If you use script in create transition place your scripted function in top of list. Add also the store issue function.

Hope that helps,

Cheers

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
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

39 views 0 3
Read article

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