Failure accessing array elements from Velocity

Lucas Layman August 26, 2015

Hi,

I am having trouble accessing array elements in a Velocity template.

I have an Entity called Hazard_Control with a method, getCauses() defined by a ManyToMany relationship, which returns Hazard_Cause[].

In my .vm file, I have the following:

#set( $mycauses = $control.getCauses())
$mycauses

Which prints out: [Lorg.fraunhofer.plugins.hts.model.Hazard_Causes;@51cf4bb2

However, when I add the line: $mycauses.get(0), I get:

com.atlassian.templaterenderer.RenderingException: java.lang.NullPointerException
	at com.atlassian.templaterenderer.velocity.one.six.internal.VelocityTemplateRendererImpl.render(VelocityTemplateRendererImpl.java:109)
	at sun.reflect.GeneratedMethodAccessor375.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
    ...


How do I access elements of this array? Converting it to a List in the Java code is not an option since, I believe, ManyToMany relationships in Entities only return arrays.

Thanks,

Lucas

=======================================

EDIT (August 27 2015): Adding more code in response to answers below:

1				#set( $mycauses = $control.getCauses())
2				#if($mycauses)
3					mycauses object: $mycauses <br/>
4					#foreach($cause in $mycauses)
5						Cause ID: $cause.getID() <br/>
6					#end
7					Access by index: $mycauses[0]
8				#else
9					Causes DO NOT exist
10				#end

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Stephen Deutsch
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.
August 27, 2015

You should probably call $mycauses.isEmpty() or $mycauses.size() to see if there are actually any causes returned by the call (which is I think why you are getting the nullpointerexception).

If you want to step through the causes, you might just use this syntax:

#foreach ( $cause in $mycauses )
  $cause
#end
Lucas Layman August 27, 2015

Hi Stephen, Thank you for the response. The #foreach loop does work, but I don't want to have to loop through this particular array because the item I need is in position 0. Whenever I call $mycauses.size() or $mycauses.isEmpty(), I receive the following: [INFO] [talledLocalContainer] com.atlassian.templaterenderer.RenderingException: java.lang.NullPointerException [INFO] [talledLocalContainer] at com.atlassian.templaterenderer.velocity.one.six.internal.VelocityTemplateRendererImpl.render(VelocityTemplateRendererImpl.java:109) .... [INFO] [talledLocalContainer] Caused by: java.lang.NullPointerException [INFO] [talledLocalContainer] at com.atlassian.templaterenderer.velocity.TemplateRendererReturnValueAnnotator.getAnnotationsForMethod(TemplateRendererReturnValueAnnotator.java:38)

Stephen Deutsch
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.
August 27, 2015

Why don't you try this then: #set ( $cause = "" ) #foreach ( $causes in $mycauses ) #set ( $cause = $causes ) #break #end

Lucas Layman August 27, 2015

Hi Stephen, Yes, I can do that. I have implemented it as a workaround. This doesn't seem like good coding practice... But more importantly, the Velocity documentation says that accessing .size() and .isEmpty() should work (as you suggested in your answer). So what is happening? Am I somehow using an outdated version of Velocity? I am using atlassian-sdk 5.0.4.

0 votes
Volodymyr Krupach
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.
August 26, 2015

Hi Lucas,

Did you try to access it by index:

$mycauses[0]
Lucas Layman August 27, 2015

Hi Volodymyr, Thank you for the response. I have added a code block to my original question. I attempt to access by index in Line 7 of the code block. The result is: Access by index: [Lorg.fraunhofer.plugins.hts.model.Hazard_Causes;@3e6f26c4[0]

TAGS
AUG Leaders

Atlassian Community Events