Getting a null pointer exception for this script - why?

Maya_Chase January 4, 2018

I have this Scriptrunner script:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor

MutableIssue issue = issue
def watcherManager = ComponentAccessor.getWatcherManager();
def userManager = ComponentAccessor.getUserManager()
def user1 = userManager.getUserByKey('flaurentine')


if (issue.getReporterId() in ['machase' <and a list of other ids>])
{
watcherManager.startWatching(user1, issue)
}

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

I get no errors when saving the script, but when I run it I get:

2018-01-04 11:07:00,157 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-01-04 11:07:00,157 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
	at com.google.common.cache.LocalCache$LocalManualCache.invalidate(LocalCache.java:4764)
	at com.atlassian.jira.issue.watchers.DefaultWatcherManager.updateWatch(DefaultWatcherManager.java:167)
	at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:84)
	at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:75)
	at com.atlassian.jira.issue.watchers.WatcherManager$startWatching.call(Unknown Source)
	at Script206.run(Script206.groovy:32)

So apparently it doesn't like my watcherManager statement, but I don't know why? I've checked and user1 is what I expect it to be. By process of elimination I'm misusing "issue" but I don't know how to fix it. Please help. Thank you!

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
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.
January 4, 2018

Hello,

Where do you execute the script?

you should log values to your log to be sure that issue and user1 are not null.

log.error(issue)

log.error(user1)

Then have a look at the logs.

Alexey Matveev
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.
January 4, 2018

I mean the variable issue is only available for post-function, validators and conditions.

Like Thomas Fischer likes this
Maya_Chase January 4, 2018

Hi Alexey, thank you so much for answering. I put in:

log.debug(user1)
log.debug(issue)

and I get back:

2018-01-04 11:51:31,700 DEBUG [jira.groovy]: flaurentine(flaurentine)
2018-01-04 11:51:31,700 ERROR [jira.groovy]: debug

 The first one is as expected, but I don't know what the other one means? 

Alexey Matveev
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.
January 4, 2018

Where do you execute the script?

try log.debug(issue.key)

I am also not sure what the second line means

Maya_Chase January 4, 2018

The script is in a post-function on the create transition. 

I took out the startWatching statement but added log.debug(issue.key) and I get this:

2018-01-04 12:18:03,426 DEBUG [jira.groovy]: flaurentine(flaurentine)
2018-01-04 12:18:03,426 DEBUG [jira.groovy]: debug
2018-01-04 12:18:03,428 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-01-04 12:18:03,429 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
 at com.google.common.cache.LocalCache$LocalManualCache.invalidate(LocalCache.java:4764)
 at com.atlassian.jira.issue.watchers.DefaultWatcherManager.updateWatch(DefaultWatcherManager.java:167)
 at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:84)
 at com.atlassian.jira.issue.watchers.DefaultWatcherManager.startWatching(DefaultWatcherManager.java:75)
 at com.atlassian.jira.issue.watchers.WatcherManager$startWatching.call(Unknown Source)
 at Script281.run(Script281.groovy:34)

So I'm back to the null pointer exception, so I'm guessing that means issue is null, or at least issue.key is null. How should I be getting issue to mean "this issue that I'm creating"?

Alexey Matveev
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.
January 4, 2018

Your post function must be last in the list

Maya_Chase January 4, 2018

That was it! Easy peasy fix, thank you!

Suggest an answer

Log in or Sign up to answer