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
Community moderators have prevented the ability to post new answers.
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
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Why don't you try this then: #set ( $cause = "" ) #foreach ( $causes in $mycauses ) #set ( $cause = $causes ) #break #end
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Lucas,
Did you try to access it by index:
$mycauses[0]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.