Handling Null Values Returned by Java

Posted on May 15, 2006

So in my recent exploits toying with javaLoader.cfc and a java library for parsing rss, I came upon some odd errors. Well, I am certainly a noob when it comes to Java, but it appears that when you call a method in java and that method returns a null, you can get some odd errors. For example, you may have code like this:

<cfset x = javaObj.getValue() /> <cfdump var="#x#">

This was returning an error that x was not defined on the cfdump. This was clearly confusing, but it appears that this was because getValue() was returning null. Well, you can wrap this in a try/catch block, or you could use a simple fix that works for methods that return simple values:

<cfset x = trim(javaObj.getValue()) /> <cfdump var="#x#">

This code will return [empty string] for null and obviously you won't get an error. Like I said, this won't work for methods that return complex objects, but it appears to be an easy fix for strings or numeric values.

Comments

Brian Rinaldi fwiw, I failed to mention that you can use isDefined() to check for null values.

Posted By Brian Rinaldi / Posted on 09/22/2008 at 5:16 PM


Write your comment



(it will not be displayed)





About

My name is Brian Rinaldi and I am the Web Community Manager for Flash Platform at Adobe. I am a regular blogger, speaker and author. I also founded RIA Unleashed conference in Boston. The views expressed on this site are my own & not those of my employer.