Uploaded image for project: 'Jira Data Center'
  1. Jira Data Center
  2. JRASERVER-63619

NullPointerException when trying to access array elements in Velocity

XMLWordPrintable

      Summary

      Calling methods like size or get on array primitives in Velocity template will throw an NPE

      Environment

      • 7.2.6 (with velocity-1.6.4-atlassian-7.jar)*

      Steps to Reproduce

      1. Create a VM template
        • Bare minimum is the following
          #disable_html_escaping()
          
          #set($array=$issue.summary.split(","))
          Size: $array.size() --
          First element: $array.get(0) --
          
      2. Replace this with any of JIRA templates (eg. <JIRA Install>/atlassian-jira/WEB-INF/classes/templates/)
      3. Render the template (eg. trigger email notification if you replaced an email template)

      Expected Results

      Array objects are accessible and method doesn't thrown an error

      Actual Results

      Error such as the following is seen in the logs

      2016-12-16 13:28:42,889 http-nio-28080-exec-18 ERROR jgerken 808x4900x1 9tmrua 95.33.69.41,127.0.0.1 /secure/admin/jeti/jetiSendTestEmail!preview.jspa [c.atlassian.velocity.DefaultVelocityManager] Exception getting message body from Velocity: java.lang.NullPointerException
      java.lang.NullPointerException
      	at com.atlassian.velocity.htmlsafe.introspection.ReturnValueAnnotator.getAnnotationsForMethod(ReturnValueAnnotator.java:38)
      	at com.atlassian.velocity.htmlsafe.introspection.AnnotationBoxingUberspect.getMethodAnnotations(AnnotationBoxingUberspect.java:292)
      	at com.atlassian.velocity.htmlsafe.introspection.HtmlSafeAnnotationBoxingUberspect.getMethodAnnotations(HtmlSafeAnnotationBoxingUberspect.java:29)
      	at com.atlassian.velocity.htmlsafe.introspection.AnnotationBoxingUberspect.getMethod(AnnotationBoxingUberspect.java:117)
      	at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:192)
      	at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:262)
      	at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:342)
      	at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:336)
      	at org.apache.velocity.runtime.RuntimeInstance.render(RuntimeInstance.java:1276)
      	at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1215)
      	at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1164)
      	at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:219)
      	at com.atlassian.velocity.DefaultVelocityManager.getEncodedBodyForContent(DefaultVelocityManager.java:205)
      	... 2 filtered
      	at java.lang.reflect.Method.invoke(Method.java:497)
      	at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
      	at com.sun.proxy.$Proxy512.getEncodedBodyForContent(Unknown Source)
      	... 2 filtered
      	at java.lang.reflect.Method.invoke(Method.java:497)
      	at com.atlassian.plugin.osgi.bridge.external.HostComponentFactoryBean$DynamicServiceInvocationHandler.invoke(HostComponentFactoryBean.java:136)
      	at com.sun.proxy.$Proxy512.getEncodedBodyForContent(Unknown Source)
      	at com.metainf.jira.plugin.emailissue.action.EmailBuilderImpl.buildEmail(EmailBuilderImpl.java:821)
      	at com.metainf.jira.plugin.emailissue.action.EmailBuilderImpl.buildEmail(EmailBuilderImpl.java:434)
      	at com.metainf.jira.plugin.emailissue.action.config.SendTestEmail.doPreview(SendTestEmail.java:123)
      	... 3 filtered
      	at java.lang.reflect.Method.invoke(Method.java:497)
      	at webwork.util.InjectionUtils$DefaultInjectionImpl.invoke(InjectionUtils.java:70)
      	at webwork.util.InjectionUtils.invoke(InjectionUtils.java:56)
      	... 2 filtered
      	at com.atlassian.jira.action.JiraActionSupport.execute(JiraActionSupport.java:63)
      	at com.metainf.jira.plugin.emailissue.action.config.TabSupport.execute(TabSupport.java:59)
      	... 7 filtered
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
      	... 50 filtered
      	at com.atlassian.greenhopper.jira.filters.ClassicBoardRouter.doFilter(ClassicBoardRouter.java:59)
      	... 66 filtered
      	at com.atlassian.jira.security.JiraSecurityFilter.doFilter(JiraSecurityFilter.java:70)
      	... 90 filtered
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      	at java.lang.Thread.run(Thread.java:745)
      

      Workaround

      To work around the problem, one can copy the elements of the array into an ArrayList:

      #macro(toList $collection)
        #set($tmp = [])
        #foreach($item in $collection)
        	#set($changed = $tmp.add($item))
        #end
        #set($collection = $tmp)
      #end
      
      #set($list = ['hello', 'world'])
      #set($arr = $list.toArray())
      #toList($arr)
      $arr.get(1)
      

              Unassigned Unassigned
              ywoo Yit Wei
              Votes:
              5 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated: