Script runner - java.lang.NullPointerException: Cannot invoke method getAt() on null object

BarL October 24, 2019

Hi,

I am keep getting this error "java.lang.NullPointerException: Cannot invoke method getAt() on null object" on the lest line on the below code "def DefectLinkedIssue = ....."

the relevant part of the code:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager

def issue = event.issue as Issue
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueManager = ComponentAccessor.getIssueManager()
def DocumentationlinkName = 'Documentation Task'

def DefectLinkedIssue = ComponentAccessor.getIssueLinkManager()?.getLinkCollection(issue, currentUser)?.getOutwardIssues(DocumentationlinkName)[0]

 

3 answers

1 accepted

0 votes
Answer accepted
fran garcia gomera
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.
October 24, 2019

the [0] is what gives you the message when there are no issues in 

ComponentAccessor.getIssueLinkManager()?.getLinkCollection(issue, currentUser)?.getOutwardIssues(DocumentationlinkName)

 this could work 

def DefectLinkedIssueCollection = ComponentAccessor.getIssueLinkManager()?.getLinkCollection(issue, currentUser)?.getOutwardIssues(DocumentationlinkName)

if (DefectLinkedIssueCollection?.size() >0) {
def DefectLinkedIssue = DefectLinkedIssueCollection[0]
}else{
// there are no links
}

 

BarL October 26, 2019

@fran garcia gomera  Thank you so much for helping me. it worked!!! 

0 votes
fjodors
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.
October 24, 2019

Hi

https://docs.atlassian.com/software/jira/docs/api/8.1.0/com/atlassian/jira/issue/link/IssueLinkType.html

Looks like getName() and getOutward() return different values.

For e.g. I have link name "clones"

getOutward() returns "clones" ,but getName() returns "Cloners"

See this code it could be helpful

 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.link.LinkCollection
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser

IssueManager im = ComponentAccessor.getComponent(IssueManager);
IssueLinkManager ism = ComponentAccessor.getComponent(IssueLinkManager);
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();


Issue createdIssue = im.getIssueByKeyIgnoreCase('<IssueKey>');

LinkCollection issueLinksCol = ism.getLinkCollection(createdIssue, currentUser);
Collection<Issue> linkedIssuesCol = issueLinksCol.getAllIssues()




def result="start...<br>";




for (Issue linkedIssue : linkedIssuesCol) {
result+=linkedIssue.getKey()+"<br>";
}

result+="-----------------------------<br>";

Set<IssueLinkType> linkTypesSet = issueLinksCol.getLinkTypes()
String linkName ='clones';
for (IssueLinkType ilt: linkTypesSet){

if(ilt.getOutward()=='clones'){
linkName=ilt.getName();
}

result+=ilt.getOutward()+"<br>";
}

result+="-----------------------------<br>";

List<Issue> myList = issueLinksCol.getOutwardIssues(linkName);

for (Issue iss : myList) {
result+=iss.getKey()+" a <br>";
}

result+="-----------------------------<br>";

result+=linkName;
0 votes
Ilya Turov
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.
October 24, 2019

hello, because this part

ComponentAccessor.getIssueLinkManager()?.getLinkCollection(issue, currentUser)?.getOutwardIssues(DocumentationlinkName)

returns you empty list, hence it can't get element at 0's key

are you sure you are looking for outward issues, or that link name is correct?

I'd recommend experimenting in script console by replasing line 

def issue = event.issue as Issue

to 

def issue = ComponentAccessor.issueManager.getIssueObject("JIRA-123")

(replace JIRA-123 with your issue key)

Suggest an answer

Log in or Sign up to answer