Script runner - NullPointerException: Cannot invoke method put() on null object Edited

Hi Community,

I've been struggling this problem for 2 days.

Here is my source code :

 

import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.util.ErrorCollection;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;

//parametrik
String testIssueTypeID = 68;
long nestPayProjectID = 10121;
long testNestPayProjectID = 13791;
long testQAProjectID = 13990;
long qaProjectID = 13093;
long cftestAkisiID = 10090;
long cfDomainsID = 13651;
long cfMainVersionID = 13552;
long cfUtilizationCategoryID = 11861;
long issueLinkDependsOnID = 10020;
long linkOrderNo = 1;

if(issue.getProjectId() == nestPayProjectID || issue.getProjectId() == testNestPayProjectID)//sadece "nestPay" veya "test nestPay" projesinde "QA projesi" issue'ları yaratılıyor.
{
IssueManager issueManager = ComponentAccessor.getIssueManager();
IssueService.CreateValidationResult createValidationResult;
IssueService.IssueResult createResult;

def created = false;
def linkManager = ComponentAccessor.getIssueLinkManager();
def issueService = ComponentAccessor.getIssueService();
def inputParameters = issueService.newIssueInputParameters();
def curAppUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

def assignee = ComponentAccessor.getUserManager().getUserByName("xyz");
def adminUser = ComponentAccessor.getUserManager().getUserByName("asd.dsa");
def cfTestAkisi = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(cftestAkisiID);
def cfDomain = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(cfDomainsID);
def cfMainVersion = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(cfMainVersionID);
def cfUtilizationCategory = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(cfUtilizationCategoryID);

List<IssueLink> issueLinkList = new ArrayList<>();
issueLinkList.addAll(linkManager.getOutwardLinks(issue.getId()));
issueLinkList.addAll(linkManager.getInwardLinks(issue.getId()));

Map<Option, Boolean> issueLinkMap;

for (Iterator<IssueLink> iterator = issueLinkList.iterator(); iterator.hasNext();) {
IssueLink issueLink = (IssueLink) iterator.next();
Issue linkedIssue;
if (issueLink.getDestinationObject().getKey().startsWith("TQA")) {
log.warn("ISSUE: " + issueLink.getDestinationObject().getKey());
linkedIssue = issueLink.getDestinationObject();
} else if (issueLink.getSourceObject().getKey().startsWith("TQA")) {
log.warn("ISSUE: " + issueLink.getSourceObject().getKey());
linkedIssue = issueLink.getSourceObject();
}
log.warn("linkedIssue : " + linkedIssue);
log.warn("linkedIssueDomains : " + cfDomain.getValue(linkedIssue))
if(linkedIssue != null){
List<Option> linkedIssueDomains = (List<Option>)cfDomain.getValue(linkedIssue);
log.warn("linkedIssueDomains : " + linkedIssueDomains);
log.warn("linkedIssueDomain : " + linkedIssueDomains.iterator().next());
Option linkedIssueDomain = linkedIssueDomains.iterator().next();
issueLinkMap.put(linkedIssueDomain, true);
}
}

List<Option> domains = (List<Option>)cfDomain.getValue(issue);
Option utilizationCategory = (Option)cfUtilizationCategory.getValue(issue);

Collection<Version> fixVersions = issue.getFixVersions();
String mainVersion = fixVersions.iterator().next().toString();
log.warn(fixVersions);
log.warn(mainVersion);

log.warn("domains: " + domains)
for (Option domain : domains) {
if(issueLinkMap != null)
created = issueLinkMap.get(domain) != null ? true : false;
log.warn("created : " + created);
if(!created)
{
inputParameters.setProjectId(testQAProjectID)
.setIssueTypeId(testIssueTypeID)
.setSummary("Test for " + domain.getValue() + ": " + issue.getSummary())
.setReporterId(issue.getReporterId())
.setPriorityId(issue.getPriority().getId())
.setDescription(issue.getDescription())
.addCustomFieldValue(cftestAkisiID, cfTestAkisi.getValue(issue).toString())
.addCustomFieldValue(cfDomainsID, domain.getOptionId().toString())
.addCustomFieldValue(cfMainVersionID, mainVersion)
.addCustomFieldValue(cfUtilizationCategoryID, utilizationCategory.getOptionId().toString());

createValidationResult = issueService.validateCreate(curAppUser, inputParameters);

log.warn(cfTestAkisi.getValue(issue));
log.warn(domain);
log.warn(issue.getFixVersions());
log.warn(utilizationCategory.getValue());
log.warn(createValidationResult.isValid());

if (createValidationResult.isValid()) {
createResult = issueService.create(curAppUser, createValidationResult);
log.warn("issue created");
def createdIssue = createResult.getIssue();
ComponentAccessor.getIssueLinkManager().createIssueLink(issue.getId(), createdIssue.getId(), issueLinkDependsOnID, linkOrderNo, adminUser);
log.warn("issue linked");
}
else {
ErrorCollection errors = createValidationResult.getErrorCollection();
errors.getErrorMessages().each{log.warn(it)};
errors.getReasons().each{log.warn(it)};
errors.getErrors().each{k,v -> log.warn("Key : " + k + " Value : " + v)};

}
}
}
}

And here is my logs :

2017-12-26 10:34:35,368 WARN [workflow.ScriptWorkflowFunction]: ISSUE: TQA-64
2017-12-26 10:34:35,370 WARN [workflow.ScriptWorkflowFunction]: linkedIssue : TQA-64
2017-12-26 10:34:35,371 WARN [workflow.ScriptWorkflowFunction]: linkedIssueDomains : [TR]
2017-12-26 10:34:35,372 WARN [workflow.ScriptWorkflowFunction]: linkedIssueDomains : [TR]
2017-12-26 10:34:35,372 WARN [workflow.ScriptWorkflowFunction]: linkedIssueDomain : TR
2017-12-26 10:34:35,375 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-12-26 10:34:35,375 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: TNP-24, actionId: 161, file: <inline script>
java.lang.NullPointerException: Cannot invoke method put() on null object
	at Script268.run(Script268.groovy:72)


Can you help me?

1 answer

1 accepted

1 vote
Alexey Matveev Community Champion Dec 26, 2017

Hello,

I think the problem is because linkedIssueDomains.iterator().next() returns null.

You log "log.warn("linkedIssueDomain : " + linkedIssueDomains.iterator().next());"

The problem is that in the log it can be not null. But when you assign it to a value it can be null because iterator().next() takes the next element each time you call it. Rewrite it 

Option linkedIssueDomain = linkedIssueDomains.iterator().next();

log.warn("linkedIssueDomain : " + linkedIssueDomain);

 and have a look if linkedIssueDomain is null or not.

Thanks for quick response.

Strange things keep happening :)

Here how i changed the if block as you showed.

if(linkedIssue != null){
List<Option> linkedIssueDomains = (List<Option>)cfDomain.getValue(linkedIssue);
log.warn("linkedIssueDomains : " + linkedIssueDomains);
Option linkedIssueDomain = linkedIssueDomains.iterator().next();
log.warn("linkedIssueDomain : " + linkedIssueDomain);
issueLinkMap.put(linkedIssueDomain, true);
}

But still i keep getting the same error.

2017-12-26 11:35:34,103 WARN [workflow.ScriptWorkflowFunction]: ISSUE: TQA-64
2017-12-26 11:35:34,105 WARN [workflow.ScriptWorkflowFunction]: linkedIssue : TQA-64
2017-12-26 11:35:34,106 WARN [workflow.ScriptWorkflowFunction]: linkedIssueDomains : [TR]
2017-12-26 11:35:34,106 WARN [workflow.ScriptWorkflowFunction]: linkedIssueDomains : [TR]
2017-12-26 11:35:34,107 WARN [workflow.ScriptWorkflowFunction]: linkedIssueDomain : TR
2017-12-26 11:35:34,109 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-12-26 11:35:34,109 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: TNP-24, actionId: 161, file: <inline script>
java.lang.NullPointerException: Cannot invoke method put() on null object
 at Script280.run(Script280.groovy:72)

 By the way Line 72 is this:

 issueLinkMap.put(linkedIssueDomain, true); 
Alexey Matveev Community Champion Dec 26, 2017

Did you check in the log file? linkedIssueDomain is null or not?

log file shows this

2017-12-26 11:35:34,107 WARN [workflow.ScriptWorkflowFunction]: linkedIssueDomain : TR 
Alexey Matveev Community Champion Dec 26, 2017

I found it. You did not initialize  Map<Option, Boolean> issueLinkMap;

You should do like this

Map<Option, Boolean> issueLinkMap = new HashMap<Option, Boolean>();

oh gosh! How can i miss that.

Alexey thanks a lot. You saved me :)

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

148 views 1 10
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