It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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
}

 

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

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)

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;

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

273 views 1 2
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you