Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using Arrays in Velocity

Lionel Hutz
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.
December 15, 2011

Is it possible to use arrays in velocity in Confluence? According to this post:

https://answers.atlassian.com/questions/317/which-version-of-velocity-is-used-for-confluence

Confluence (I'm on 3.5.x) is using velocity 1.6.1, which according to this:

http://stackoverflow.com/questions/191844/what-is-the-best-way-to-access-an-array-inside-velocity

says I should be able to use $myArray.get($index), but this is giving me an odd stack trace:

java.lang.NullPointerException
    at com.atlassian.confluence.velocity.introspection.ReturnValueAnnotator.getAnnotationsForMethod(ReturnValueAnnotator.java:18)

I'll file a bug report if necessary but wanted to check here first to see if I was way off base.

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Thomas Wendel
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 22, 2012

I get the same error in Confluence 4.1.

However, if you convert the array into a list, $myListThatWasAnArray.get($index) works fine.

taidt1 January 2, 2019

Do you know how to convert the array into list directly on Velocity file. It would be helpful for all of us when we want to modify the core velocity file and add some logics. :D Thank you

0 votes
taidt1 January 2, 2019

You can use this trick directly on velocity file to get the ith element, or get the size of the array. In this code example, I take the first element of the array

 

 

 

#set ($counter = 0)

#set ($value = "")

#foreach ($item in $array)

         #if($counter == 0)

              #set ($value = $item)

         #end

         #set ($counter = $counter +1)

#end

0 votes
JenniferC May 28, 2012

I'm getting the same in Confluence 4.2 trying to write a user macro. Very frustrating. I can only look through the array using #foreach but can't use any of the List methods that are apparently supported in this version of Velocity.

TAGS
AUG Leaders

Atlassian Community Events