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

ShamhoonJ May 24, 2013

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

0 votes
Tsol
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.
June 3, 2013

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

0 votes
HermannS June 3, 2013

Hi

How do you try to run that script?

And at which line do you get the NullPointerException?

Greetings

Hermann

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events