Mark Drew (Redux)- cf_etc...

a compendium of railo, cfml, cfeclipse and technology topics

Mark Drew (Redux)- cf_etc...

Coldfusion handling Java null's

July 12, 2006 · 9 Comments

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.

Tags: coldfusion · reference · webdev

9 responses so far ↓

  • 1 Justin Wilson // Sep 22, 2008 at 4:10 PM

    &lt;cffunction name=&quot;createJavaNull&quot; access=&quot;private&quot; output=&quot;false&quot; hint=&quot;Creates a null value.&quot;&gt;
       &lt;cfset vector = CreateObject(&quot;java&quot;, &quot;java.util.Vector&quot;)&gt;
       &lt;cfset vector.setSize(1)&gt;
       &lt;cfreturn vector.get(0) /&gt;
    &lt;/cffunction&gt;

    I found this technigue, can be very usefull!
  • 2 Kurt Wiersma // Sep 22, 2008 at 4:11 PM

    I have always used isDefined(&quot;javaReturnVal&quot;) to test to see if javaReturnVal is null.
  • 3 Mark Drew // Sep 22, 2008 at 4:11 PM

    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.
  • 4 Vince Bonfanti // Sep 22, 2008 at 4:11 PM

    Hi Mark,

    You might be interested to know that we recently announced that BlueDragon 7.0 will include both support for &quot;null&quot; 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.
  • 5 Mark Drew // Sep 22, 2008 at 4:11 PM

    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.
  • 6 Damon Cooper // Sep 22, 2008 at 4:11 PM

    Starting with CF7, the CFML JavaCast function supports Nulls. See the reference for details: http://livedocs.macromedia.com/coldfusion/7/htmldocs/00000542.htm

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

    For example

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

    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
  • 7 Mark Drew // Sep 22, 2008 at 4:12 PM

    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)
  • 8 Matthew giles // Sep 22, 2008 at 4:12 PM

    This really makes life easier when working with Java methods from CFML..
  • 9 Cliff // Sep 22, 2008 at 4:13 PM

    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 &lt;cfinvokeargument name=&quot;a&quot; omit=&quot;Yes&quot;&gt; but I never use &lt;cfinvoke&gt;

    e.g.
    &lt;cfif arguments.V eq &quot;&quot;&gt;
    &lt;cfset a = ws.BP( P: arguments.P, V: arguments.V )&gt;
    &lt;cfelse&gt;
    &lt;cfset a = ws.BP( P: arguments.P, V: JavaCast( &quot;null&quot;, &quot;&quot; ) )&gt;
    &lt;/cfif&gt;

Leave a Comment