CamJohnson26 CamJohnson26 - 4 months ago 23
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="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/current/mule-quartz.xsd">
<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">
<quartz:event-generator-job>
<quartz:payload>foo</quartz:payload>
</quartz:event-generator-job>
</quartz:inbound-endpoint>
<message-properties-transformer doc:name="Message Properties">
<add-message-property key="crmRequests" value="#[[]]"/>
</message-properties-transformer>
<dw:transform-message doc:name="Copy_of_buildUpdateRequest">
<dw:set-payload><![CDATA[
%dw 1.0
%output application/java
---
{
statecode: 0
}
]]></dw:set-payload>
</dw:transform-message>
<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"/>
</flow>
</mule>

Answer

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)

Comments