How to pass paremeters into Velocity

 have method in my Java class.

public String getCorrectFormatDate(Issue issue){
    Long timespent = issue.getTimeSpent();

    if(timespent>0){
        int hours = (int) (timespent / 3600);
        int remainder = (int) (timespent - hours * 3600);
        int min = remainder / 60;
        String result = hours + "h " + min +"min";
        return result;
    }
    else{
        return "0";
    }

}

Also, In my velocity template I have this code

#foreach ($issue in $issues)
        <tr role="row">
            <td colspan="1" class="confluenceTd"><a href="$action.getBaseURL()/browse/${issue.getKey()}">$issue.getKey()</a></td>
            <td colspan="1" class="confluenceTd">$issue.getSummary()</td>
            <td colspan="1" class="confluenceTd">$action.getCorrectFormatDate($issue)</td>
        #end

Why this my certain method isnt working?

$action.getCorrectFormatDate($issue)

threw exception java.lang.NullPointerException

1 answer

1 accepted

0 votes
Answer accepted
Tom Lister Community Champion Nov 02, 2018

Hi @Stephen Wolf

Are you passing an instance of your class into velocity params with key action?

What kind of plugin are you developing?

"Are you passing an instance of your class into velocity params with key action?"

What does it mean? 

In my velocity I use 

$webResourceManager.requireResourcesForContext

 
And I have a properly configuration in xml, I can invoke method without parameters, but in this case I want to in Velocity pass parameter and theres a problem

Tom Lister Community Champion Nov 04, 2018

Hi Steven

 

Forget my previous comments, I think the problem here is that you have issues without work logged against them. In those cases the getTimeSpent method will return null

 

If that's not the case, have you got more info?

Can you post a fuller stack trace that shows the error class and line number reported? That will tell me more.

What is your Java Class? Have you extended an action and expect that action class to be available in the template?

Hello, 

For example, If I will use;


<td colspan="1" class="confluenceTd">$issue.getTimeSpent()</td>

 

I will get a result in miliseconds, so it seems that the parameter is passed. 

Now, I want to use method from my Java class, because I want get time in nice format hh:mm. 

In my Java class I have the following method. 

public String getCorrectFormatDate(Issue issue){
Long timespent = issue.getTimeSpent();

if(timespent>0){
int hours = (int) (timespent / 3600);
int remainder = (int) (timespent - hours * 3600);
int min = remainder / 60;
String result = hours + "h " + min +"min";
return result;
}
else{
return "0";
}

}

 

Also, In my template I pass variable 'issues' from my Java class from other method. 

 

Im sure, that This template see my Java class with this method 

public String getCorrectFormatDate(Issue issue)

 

Problem is, that in my Velocity template I want to pass parameters 

for exmaple 

#foreach ($issue in $issues)

<tr role="row">
<td colspan="1" class="confluenceTd">$action.getTimeSpent($issue)</td>

#end 


How to solve my problem and call function for each issue, 
Thanks a lot for Your helps, Best wishes

Tom Lister Community Champion Nov 05, 2018

Hi Stephen

Are you still getting the NullPointerException?

The only place I can deduce will return null in the code you've shown is the issue.getTimeSpent() call in your getCorrectFormatDate method. (I assume that is in your own action class that is preparing the velocity call)

Are you able to add logging or run in debug?

 

I'm seeing now that you are in Confluence not JIRA. The $action object is the current webwork action. Is your method in your webwork action class?

If not, then you need to pass your class in the velocity context as per the example in your link

e.g.

Message mg = new Message(); 

    context.put("formatter", mg);

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 14, 2019 in Jira

Updates to jira.atlassian.com give you visibility into what's coming in Jira Server and Data Center

Hello, Community! My name is Gosia and I'm a Product Manager on Jira Server and Data Center here at Atlassian. Since 2002 when we launched our public issue tracker, jira.atlass...

720 views 1 15
Read article

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