Question

I am trying to get a substring but I get a Failed to execute the [velocity] macro. I have no idea why this is happening. Here is my code:

#if ($text != '' )
   #set ($textwo = "${text.substring(0,4)}")
   if($textwo != "Tags" )
      #getSearchResults()
      (% class="search-results-container" %)(((
         #displaySearchFacets($searchResponse)
         (% class="search-results-left" %)(((
            #displaySearchResultsSort()

            #displaySearchResults()
            )))
          )))
  #end
  #if ($textwo == 'Tags')
    #getTaggedResult
  #end
#end

If I remove the line #set ($textwo = "${text.substring(0,4)}"), I dont get the error anymore and ofcourse it does not work.

Was it helpful?

Solution

If you click on the //Failed to...// error message, it will expand to show the full stack trace. You'll probably find that the real cause of the error is:

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 4

Which means that the input string is shorter than 4 characters.

What is $text supposed to be? Why are you keeping only the first four characters? Would #if ($text.startsWith('Tags')) be an equivalent check?

In XWiki you can also use $stringtool, which is actually StringUtils from Apache Commons Lang 3, and which has error-preventing methods for extracting a piece of text, or extracting a prefix, or checking for a prefix, among others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top