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
Hi @lpopek
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think I have problem like this
https://stackoverflow.com/questions/40643537/pass-java-function-in-velocity-template
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.