CamJohnson26 CamJohnson26 - 1 year ago 85
Java Question

Why do I get a Runtime Exception Accessing the size of an ArrayList from MEL?

In Mulesoft I have an ArrayList stored as an outbound property with one element. It looks like this:
enter image description here

But if I try to access the size of this array I get an error and can't figure out why:
enter image description here

The error is

[Error: object is not an instance of declaring class]
[Near : {... message.outboundProperties.crm ....}]
[Line: 1, Column: 1]

And here's a flow that throws the same error:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="" xmlns:quartz="" xmlns="" xmlns:doc=""
<quartz:connector name="Quartz" validateConnections="true" doc:name="Quartz"/>
<flow name="tempFlow">
<quartz:inbound-endpoint jobName="job1" repeatInterval="1" repeatCount="0" connector-ref="Quartz" name="runOnce" doc:name="Quartz">
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="crmRequests" value="#[[]]"/>
<dw:transform-message doc:name="Copy_of_buildUpdateRequest">
%dw 1.0
%output application/java
statecode: 0
<expression-component doc:name="Copy_of_Expression"><![CDATA[#[message.outboundProperties.crmRequests.add(payload)]]]></expression-component>
<set-payload value="#[message.outboundProperties.crmRequests.size()]" doc:name="Set Payload"/>
<logger level="INFO" doc:name="Logger"/>

Answer Source

Wrap the expression inside bracket:

<set-payload value="#[(message.outboundProperties.crmRequests).size()]" doc:name="Set Payload"/>

For further testing you can try other java.util.ArrayList methods, e.g.: (message.outboundProperties.crmRequests).get(0)