Getting a null pointer exception for this script - why?

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
Alexey Matveev Community Champion Jan 04, 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 Community Champion Jan 04, 2018

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

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 Community Champion Jan 04, 2018

Where do you execute the script?

try log.debug(issue.key)

I am also not sure what the second line means

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 Community Champion Jan 04, 2018

Your post function must be last in the list

That was it! Easy peasy fix, thank you!

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,329 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot