Coldfusion handling Java null's

Posted At : July 12, 2006 5:28 PM | Posted By : Mark Drew
Related Categories: coldfusion, Java, reference, webdev

I have been reading a number of posts how to handle nulls in Coldfusion. Coldfusion doesnt naturally handle a "null" object returned from Java (as far as I can see, I have read a few posts and still no definate answer, so here goes for a couple of tips, just to keep myself sane and to remind myself.

There are two ways that you can handle a null and it depends what the returned object is, firstly lets handle nulls where the returned object might be something simple like a number or a string, the easiest method would be to simplify the variable, so you can trim the result, if it is null, you will get an empty string:

<cfset myString = CreateObject("java", "com.some.Class").init().getName()>

<cfif Len(myString)>
#myString#
</cfif>

The second method is used if you return complex data, such as an Array, Structure or Java Objects themselves:

<cfset UsersArray = CreateObject("java", "com.some.Class").init().getUsers()>

<cfif isDefined("UsersArray")>
<cfdump var="#UsersArray#">
</cfif>

If someone has a better method of managing returned nulls from Java, please let me know and I can update this post.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Kurt Wiersma's Gravatar I have always used isDefined("javaReturnVal") to test to see if javaReturnVal is null.
# Posted By Kurt Wiersma | 7/12/06 7:01 PM
Mark Drew's Gravatar Kurt, that might do the trick in itself. I think it would be nice to have an isNull() function in coldfusion to handle this kind of thing, it would just help integrating with not-so-nice Java applications a little bit more.
# Posted By Mark Drew | 7/12/06 7:26 PM
Vince Bonfanti's Gravatar Hi Mark,

You might be interested to know that we recently announced that BlueDragon 7.0 will include both support for "null" as a keyword in CFML expressions and an isNull() function. I demonstrated these during one of my presentations at CFUNITED, and will put more info up on my blog in the next week or two.
# Posted By Vince Bonfanti | 7/12/06 9:33 PM
Mark Drew's Gravatar Hi Vince! Sorry I kept missing you at CFunited! Would have been great to catch up with you. That would be (at this moment) a very useful function! I look forward to your posting.
# Posted By Mark Drew | 7/13/06 6:26 AM
Damon Cooper's Gravatar Starting with CF7, the CFML JavaCast function supports Nulls. See the reference for details: http://livedocs.macromedia.com/coldfusion/7/htmldo...

This really makes life easier when working with Java methods from CFML.

For example

x = CreateObject("java", "test.Hello");
x.init();
ret = x.sayHello(JavaCast("null", ""));

There's obviously a lot more to working with with nulls, but this was a quick little feature we added in CF7 that addresses the most commonly reported annoyance by folks working with Java and CFML together.

Feedback welcome!

Damon
# Posted By Damon Cooper | 7/13/06 1:54 PM
Mark Drew's Gravatar That is definately a useful function Damon, and I guess that is how it should be since the null is a Java artifact rather than a Coldfusion one. The problem I am having generically is checking for Java Nulls that are returned. I think I just have to handle stuff carefully that comes back from java (oh how I love those NPE's)
# Posted By Mark Drew | 7/14/06 1:41 PM
Matthew giles's Gravatar This really makes life easier when working with Java methods from CFML..
# Posted By Matthew giles | 7/20/06 8:41 AM
Cliff's Gravatar A little off topic but I use the JavaCast function when calling a webservice that has a nullable argument. I found out later that you can do it with <cfinvokeargument name="a" omit="Yes"> but I never use <cfinvoke>

e.g.
<cfif arguments.V eq "">
<cfset a = ws.BP( P: arguments.P, V: arguments.V )>
<cfelse>
<cfset a = ws.BP( P: arguments.P, V: JavaCast( "null", "" ) )>
</cfif>
# Posted By Cliff | 6/5/07 1:36 PM
Justin Wilson's Gravatar <cffunction name="createJavaNull" access="private" output="false" hint="Creates a null value.">
   <cfset vector = CreateObject("java", "java.util.Vector")>
   <cfset vector.setSize(1)>
   <cfreturn vector.get(0) />
</cffunction>

I found this technigue, can be very usefull!
# Posted By Justin Wilson | 6/6/07 8:35 PM
# Posted By Ryan | 10/11/07 2:33 AM