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,314 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
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
Jun 19, 2013 at 9:35 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Joe, Oh, super interesting! I had only thought to url-encode the signature; but I think that's because the S3 docs actually have a special NOTE telling you to do so. It would have never occurred t ... read »
Jun 19, 2013 at 9:32 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Richard, Glad you like! Hopefully I'll have some more interesting stuff coming. This morning, I blogged a bit more about generating the pre-signed, query string authenticated URLs; but, then deeme ... read »
Jun 19, 2013 at 9:31 PM
Filter vs. ngHide With ngRepeat In AngularJS
@Mike, Honestly, in the majority of cases, I would say there isn't going to be a difference. Both approaches have trade-offs. If you use the filter, then you have fewer DOM elements and fewer $scop ... read »
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools