Warning: A non-numeric value encountered in /home/customer/www/archive.ricston.com/public_html/wp-content/themes/Divi/functions.php on line 5766

When Mule invokes a method that returns a null, is this the same thing as invoking a void method? I didn’t think so and I wanted to write this blog post to document the difference in these two scenarios.

Consider the following class:

public class MessageFlowClass {

	public void fullName (String name) {
		name = "Antoine Borg";
	}

	public Object fullName (Object nameObj) {
		if (nameObj instanceof String) {
			return nameObj;
		} else {
			return null;
		}
	}
}

It’s simple enough and has two methods that will demonstrate the difference between returning null and not returning anything at all. I put together a simple Mule configuration file with an inbound and outbound endpoint and no routers. (I also used asynchronous VM). My first test case looks like this:

public void testSimpleResponse () {
	MuleMessage aReply = null;
	try {
		MuleClient myClient = new MuleClient ();
		myClient.dispatch("fromTest", new
			DefaultMuleMessage ("Antoine"));
		aReply = myClient.request("toTest", 1000);
	} catch (MuleException e) {
		fail (e.getDetailedMessage());
	}

	assertNull (aReply);
}

I’m causing the first method to be invoked. Since this method is a void method, there is no return value and therefore there shouldn’t be anything passed on to the Outbound Flow Stage. My assertion verifies that this is the case.

I then created another test:

public void testNullResponse () {
	MuleMessage aReply = null;
	try {
		MuleClient myClient = new MuleClient ();
		myClient.dispatch("fromTest", new
			DefaultMuleMessage (new Object()));
		aReply = myClient.request("toTest", 10000);
	} catch (MuleException e) {
		fail (e.getDetailedMessage());
	}
	assertNotNull (aReply);
	assertNotNull (aReply.getPayload());
	assertTrue(aReply.getPayload () instanceof NullPayload);
}

As you can see here, when a method returns null, it does not mean that there is no MuleMessage – on the contrary, there is a MuleMessage that has a null payload. Effectively, Mule is saying, “There was a response and the response was Null”.

Conclusion: When your classes return a null value, it does not mean that there is no MuleMessage. This message will be picked up by your outbound routers and, unless you’re filtering for nulls, will be sent along the next endpoint(s). This could potentially cause problems later on in your scenario.