How to retrieve details from a current issue?

Sharath T S January 26, 2015

Hi All,

I am trying to retrieve summary of current issue and display it on an issue tab panel.

this is how i am doing it...

public String retSummary(){
		JiraHelper jiraHelper = new JiraHelper();
		Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue");
		String summary = currentIssue.getSummary();
		return summary;
	}
 
public void populateVelocityParams(Map params){
params.put("issue", this);
}

and in the velocity template, if i call $issue.retSummary().

i am getting the bellow error in the issue tab panel

An error occurred whilst rendering this message. Please contact the administrators, and inform them of this bug. Details: ------- org.apache.velocity.exception.MethodInvocationException: Invocation of method 'retSummary' in class com.sts.tabpanel.Velocity threw exception java.lang.NullPointerException at com.sts.tabpanel.tabpanel.test4:perforce/templates/tabpanels/perforce.vm[line 15, column 37] at org.apache.velocity.runtime.parser.node.ASTMethod.handleInvocationException(ASTMethod.java:337) at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:284) at 

and so on...

What mistake have i done? or is it the right way of retrieving current issue details?

if not, please suggest the right way of doing it.

 

Many thanks,

Sharath TS 

9 answers

1 accepted

0 votes
Answer accepted
Sharath T S January 29, 2015

Hi All,

Thanks you very much for your valuable inputs. 

i finally solved it.

In The main class which extends AbstractIssueTabPanel and implements IssueTabPanel,

In getAction method, we should pass  "issue.getSummary()" to the velocity class! like the below line.

return Collections.singletonList(new Velocity(super.descriptor, issue.getSummary()))

 

then in velocity constructor, initialize variable this.summary = summary

and params.put("summary", summary)

0 votes
Sharath T S January 29, 2015

yes, this String summary = currentIssue != null ? currentIssue.getSummary() : ""; code works and ignores NPE. error. but the context here is, lets say i go to a view page of issue TEST-1, which has the issuetabpanel that i have created. In that issue tab panel i need to display details(ex-summary) of TEST-1, likewise details of TEST-2 in its issuetabpanel and so on... the jiraHelper is not fetching details from the current view page of an issue!

0 votes
Volodymyr Krupach
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 28, 2015

Sure it's correct. Null means that there is no an issue in the context when a method is invoked. Did you try to add a check for a null as I posted in the answer. It can happen that the method is invoked couple times and when it's invoked first teim there is no issue in the context.

0 votes
Sharath T S January 28, 2015

Hi,

Could not obtain the current issue details through java but found a workaround through javascript from the below link

https://answers.atlassian.com/questions/117504

 

Thank you for your inputs.

An answer on how to tackle the NPE or why NPE is showing up in the above java code will be much appreciated!

0 votes
Sharath T S January 28, 2015

yes, it is the problem with null. is the code correct? Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue"); especially .get("issue")

0 votes
Volodymyr Krupach
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 28, 2015

One more though. If it's a case with null issue, you can adjust the code:

Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue");
        String summary = currentIssue != null ? currentIssue.getSummary() : "";
        return summary;
0 votes
Volodymyr Krupach
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 28, 2015

I guess that (Issue) jiraHelper.getContextParams().get("issue"); return null and currentIssue.getSummary(); crashes with NullPointerException. Please try to debug the method.

0 votes
Jobin Kuruvilla [Adaptavist]
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 27, 2015

retSummary? Seems you have a typo in there!

Florian Marquardt January 27, 2015

No. retSummary() is the method given in the code block

Sharath T S January 27, 2015

Florian is right, its just a method returning a String.

Jobin Kuruvilla [Adaptavist]
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 28, 2015

Sorry, I didn't notice, my bad! Can you use a different variable name instead of issue? I wonder if issue is in the context already or not. Also, I would check inside the retSummary method to make sure it is successfully retrieving summary and there are no NPEs in that method.

0 votes
Florian Marquardt January 27, 2015

why don't you directly return the issue.

I'd assume that it's not a good idea to return the whole object with 'this'

Try putting the issue or the summary in the map (e.g. params.put("issue", issue) or params.put("summary", summary) )

and then access your data in the template like:
<p>Summary :$summary</p>

 

public void populateVelocityParams(Map params){
        JiraHelper jiraHelper = new JiraHelper();
        Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue");
        String summary = currentIssue.getSummary();
		params.put("summary", summary);
}
Sharath T S January 27, 2015

Hi Florian, Thanks for your reply. I did try doing the way you have mentioned. When it is done this way the whole Activity tab panels disappears! So i am guessing there is some problem in the below lines of code...? JiraHelper jiraHelper = new JiraHelper(); Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue"); String summary = currentIssue.getSummary();

Sharath T S January 27, 2015

FYI...the class extends AbstractIssueAction

Florian Marquardt January 27, 2015

The three lines above seems to be allright. Did you got any error messages in the log/console ?

Sharath T S January 28, 2015

The code is returning NULL, any idea?

Suggest an answer

Log in or Sign up to answer