Pete Freitag Pete Freitag

Null Java References in CF 6 vs 7

Published on January 10, 2006
By Pete Freitag
coldfusionjava

ColdFusion 7 appears to be much better at passing null values to a java object. ColdFusion 6 however likes to block you from doing it, even when you want to.

I am tring to work with a java API that requires me to pass a null value to a method. Since CFMX 6 has no way of creating a null value I though I would use the Java API to concoct one. So I did something like this:

vector = CreateObject("java", "java.util.Vector");
vector.setSize(1);
someAPI.methodThatRequiresANull(vector.get(0));

This works on ColdFusion 7, but on ColdFusion 6 it throws an exception:

The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code.

Null Pointers are another name for undefined values.

This trick isn't really useful because in ColdFusion 7 you can create a null value by calling JavaCast and using "null" as the type.

For example:

someAPI.methodThatRequiresANull( JavaCast("null", "") );

Does anyone know of a way to pass nulls that works in CF 6? I know I could just write some java, but I want the solution to be pure CFML in this case.



java null cfobject createobject coldfusion cfml

Null Java References in CF 6 vs 7 was first published on January 10, 2006.

If you like reading about java, null, cfobject, createobject, coldfusion, or cfml then you might also like:

FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.

CFBreak
The weekly newsletter for the CFML Community


Comments

Pete - I've done quite a bit of work with Java and CF 6.1 (and 7). From everything I've seen, there is absolutely no way to pass a null value into a java method or constructor using ColdFusion 6.1. Sorry.
Does this need to be in one CFC? If not, you could create a java class which acts as a facade for your functionality.
by Doug Hughes on 01/10/2006 at 4:04:27 PM UTC
Hi Pete, Did you ever find a work-around for this? I have just run into the same issue. Works fine in 7, but someone has downloaded my cfc and is running it on 6.1 and the javacast to null is failing.
by Michael Traher on 04/24/2006 at 4:40:11 PM UTC
Hi Michael, no I never did find a workaround. You could perhaps use this technique: http://www.petefreitag.com/item/447.cfm but its not really a workaround.
by Pete Freitag on 04/24/2006 at 4:57:09 PM UTC
When I worked on my image manipulation component, I had to pass in a null value of an ImageObserver object type. So I created a dummy object of a button (implemented ImageObserver interface) and passed it in like this:
<cfset variables.imageObserver = createObject("java", "java.awt.Button").init()/>

I haven't tested yet, but I would think a generic one like this would work for all object types CreateObject('java', 'java.lang.Object').
by Johnny on 05/18/2006 at 8:20:49 PM UTC