How to pass paremeters into Velocity

lpopek November 2, 2018

 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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 2, 2018

Hi @lpopek

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

What kind of plugin are you developing?

lpopek November 3, 2018

"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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 4, 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?

lpopek November 5, 2018

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 Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 5, 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?

Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 5, 2018

 

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