Setting ColdFusion Query Cell Values With JavaCast() Fixes Errors

Posted September 5, 2006 at 2:20 PM by Ben Nadel

Tags: ColdFusion

I LOVE ColdFusion query of queries. They rock. They do, however, present some problems from time to time. One problem that I have encountered is that errors are thrown when running a query of queries on a query that has been manually updated. Take for example, this snippet of code:

  • <!--- Loop over users. --->
  • <cfloop query="qUser">
  •  
  • <!--- Check for participation. --->
  • <cfif qUser.pickup_count>
  •  
  • <!--- The user did participate in the time span. --->
  • <cfset qUser[ "did_participate" ][ qUser.CurrentRow ] = 1 />
  •  
  • </cfif>
  •  
  • </cfloop>

Here, I am merely looping over a query and setting a field value to 1 (defaulted to zero in original query) if the given user has at least one pickup (in a recycling program). Then, afterwards, if I run this query of queries (modified for example):

  • <cfquery name="qUserOverview" dbtype="query">
  • SELECT
  • (
  • COUNT( * )
  • ) AS participating_user_count
  • FROM
  • qUser
  • WHERE
  • did_participate = 1
  • </cfquery>

... This MIGHT throw the error:

Error casting an object of type to an incompatible type. This usually indicates a programming error in Java, although it could also mean you have tried to use a foreign object in a different way than it was designed.

The problem stems from the fact that the query of queries is having trouble figuring out the data type of the "1" I was setting into the query cell. It wants to see an integer value (of the original default value) and is having trouble converting the "1" into an "int" value. This is extremely frustrating as it makes no sense from a user standpoint. The problem, as I just stumbled upon, can be easily fixed with a JavaCast():

  • <!--- The user did participate in the time span. --->
  • <cfset qUser[ "did_participate" ][ qUser.CurrentRow ] =
  • JavaCast( "int", 1 )
  • />

If I re-run the query of queries now, no problem what so ever. I think the thing to take away here is to remember that underneath the surface, the Query object is really a special form of Java result set that contains Java values. If we need to set value of a cell, we are setting Java values and must be sending values via JavaCast() to ensure logical data type conversion. Of course, the same thing could be said about anthing in ColdFusion, but for some reason, this rings most true with query objects.




Reader Comments

Mar 18, 2008 at 11:15 AM // reply »
19 Comments

Thanks for this Ben. Solved a tricky issue for me. Also note that the same applies to QueryAddColumn(query, columnname, array) code, so you need to make sure all the elements in the array are also Javacast-ed. Also, I think this has been fixed in CF8.


Dec 8, 2008 at 5:04 PM // reply »
1 Comments

JavaCast worked for my problem. Thanks Ben!


Dec 8, 2008 at 5:11 PM // reply »
11,243 Comments

@Anthony,

Awesome man, glad to help.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
May 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools