null pointer exception when trying to update multiple issues

Tomáš Vrabec
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.
August 3, 2021

Hello everyone,

will try to be quick.

I trying following:

  1. We got JQL query
  2. All issues are checked
  3. We find Application listed in Insight (CF 17203) - insight object(s)
  4. We find related Business Owner
  5. We find Jira User for the Business Owner
  6. We put the Business Owner into (CF 17702) - multi user picker

The code we are running is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.user.ApplicationUser

def searchQuery = "project = RFC AND resolution = Unresolved"
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.issueManager
def userAuth = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def query = jqlQueryParser.parseQuery(searchQuery)
def searchResults = searchProvider.search(SearchQuery.create(query, userAuth), PagerFilter.unlimitedFilter)
def issueService = ComponentAccessor.issueService
def customField = null
def user = null
def userAccount = null
def key = null
def issue = null
def application = null 
ArrayList userAccounts = []
def changeHolder = new DefaultIssueChangeHolder()
searchResults.results.each 
{
documentIssue ->
userAccounts = []
key = documentIssue.document.fields.find { it.name() == "key" }.stringValue()
issue = issueManager.getIssueObject(key)
application = issue.get("customfield_17203")?.first()
customField = customFieldManager.getCustomFieldObject("customfield_17702")  
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass)
	if (application != null) { 
	int userAttributeId = 328 // Business Owner
	def persBean = objectFacade.loadObjectAttributeBean(application.getId(), userAttributeId)
		if (persBean != null) {
		user = objectFacade.loadObjectBean(persBean.getObjectAttributeValueBeans()[0].getValue())
			if (user != null) {          
			def userBean = objectFacade.loadObjectAttributeBean(user.getId(), 167) // 
			if (userBean != null) {
				def userName = userBean.getObjectAttributeValueBeans()[0].getValue()     
				if (userName != null) {
				userAccount = ComponentAccessor.getUserManager().getUserByName(userName)              
                userAccounts.add(userAccount)               
				 customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), userAccounts), changeHolder)                                 
} } } } } }

The error we got is:

Message:
java.lang.NullPointerException
Stack:
com.atlassian.jira.issue.customfields.impl.MultiUserCFType.getChangelogValue(MultiUserCFType.java:159)
com.atlassian.jira.issue.customfields.impl.MultiUserCFType.getChangelogValue(MultiUserCFType.java:101)
com.atlassian.jira.issue.fields.ImmutableCustomField.getChangelogValue(ImmutableCustomField.java:376)
com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:411)
com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
script16280233284091635377318$_run_closure1.doCall(script16280233284091635377318.groovy:49)
sun.reflect.GeneratedMethodAccessor20919.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:263)
groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1041)
groovy.lang.Closure.call(Closure.java:405)
groovy.lang.Closure.call(Closure.java:421)
org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2330)
I checked everything inside, and all variable looks fine. We just spent like 4 hours in two men on that, so the community is our last hope. Thanks guys :-)

1 answer

0 votes
Tomáš Vrabec
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.
August 5, 2021

@Jamie Echlin _ScriptRunner - The Adaptavist Group_ You are my long-term-groovy-god ... do you have any idea? :-( Spent a lot of time here, without success :-( 

Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 5, 2021

Ha... when I'm summoned like that I have no choice but to try to help ;-)

But I don't really know, what Jira version are you using so I can make sure I can read the stack trace properly? Looking at the code it's hard to see why you would get an NPE there, but maybe my line numbers are wrong, I happen to be looking at jira 8.13 source.

Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 5, 2021

I think the issue will be that:

userAccount = ComponentAccessor.getUserManager().getUserByName(userName)

in the line above, `userAccount` is null.

Because I can repro a similar stack trace using:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def issue = ComponentAccessor.issueManager.getIssueObject('JRA-1')

def customField = ComponentAccessor.customFieldManager.getCustomFieldObjectByName('MultiUserPickerA')

def user = ComponentAccessor.userManager.getUserByName('IDontExist')
customField.updateValue(null, issue, new ModifiedValue(null, [user]), new DefaultIssueChangeHolder())

 

If I change "IDontExist" to a valid user name it works.

So I suspect that you need to check whether `getUserByName` returns null or not, if it's null don't add it to the list.

I can't remember whether Insight attributes can be typed, if it's typed as User possible you want to look the user up by user key, not user name...

Tomáš Vrabec
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.
August 5, 2021

Oh my ...

I got blinded after 8 hours of looking into code.

Of course the line

if (userAccount != null) { ... }

solved the issue ... 

Thanks so much

Suggest an answer

Log in or Sign up to answer