Link to home
Start Free TrialLog in
Avatar of R_Hos
R_Hos

asked on

Object of type class java.lang.Boolean cannot be used as an array Error

I have a very frustrating problem with this cfc, it works as it is designed when updating a quantity when it is greater than 0 but the seccond that the quantity is set to zero i get the "Object of type class java.lang.Boolean cannot be used as an array".  what is really interesting is that it happens on two diffrent lines.  -- MARKED AS SUCH --

it seems as though the problem only occurs where the code loops thru the array, perhaps i've set some variable wrong, but even then it shouldnt work for any quantity.  

Thank you for any help in advance.  


<CFCOMPONENT>
  <!--- Initialize the cart's contents --->
  <!--- Because this is outside of any <CFFUNCTION> tag, --->
  <!--- it only occurs when the CFC is first created --->
  <CFSET This.CartArray = ArrayNew(1)>


  <!--- *** ADD Method *** --->
  <CFFUNCTION
    NAME="Add"
    HINT="Adds an item to the shopping cart">
    <!--- Two Arguments: ProductID and Quantity --->
    <CFARGUMENT NAME="ProductID" TYPE="numeric" REQUIRED="Yes">
    <CFARGUMENT NAME="Quantity" TYPE="numeric" REQUIRED="No" DEFAULT="1">

    <!--- Get structure that represents this item in cart, --->
    <!--- then set its quantity to the specified quantity --->
    <CFSET CartItem = GetCartItem(ProductID)>
    <CFSET CartItem.Quantity = CartItem.Quantity + Arguments.Quantity>
  </CFFUNCTION>  
 
 
  <!--- *** UPDATE Method *** --->
  <CFFUNCTION
    NAME="Update"
    HINT="Updates an item's quantity in the shopping cart">
    <!--- Two Arguments: ProductID and Quantity --->
    <CFARGUMENT NAME="ProductID" TYPE="numeric" REQUIRED="Yes">
    <CFARGUMENT NAME="Quantity" TYPE="numeric" REQUIRED="Yes">

    <!--- If the new quantity is greater than zero --->  
    <CFIF Quantity GT 0>
       <!--- Get structure that represents this item in cart, --->
       <!--- then set its quantity to the specified quantity --->
      <CFSET CartItem = GetCartItem(ProductID)>
      <CFSET CartItem.Quantity = Arguments.Quantity>
      <!--- If new quantity is zero, remove the item from cart --->
    <CFELSE>
      <CFSET This.Remove(ProductID)>
    </CFIF>
  </CFFUNCTION>  

 
  <!--- *** REMOVE Method *** --->
  <CFFUNCTION
    NAME="Remove"
    HINT="Removes an item from the shopping cart">
    <!--- One Argument: ProductID --->
    <CFARGUMENT NAME="ProductID" TYPE="numeric" REQUIRED="Yes">

    <!--- What position is this item occupying in the cart? --->
    <CFSET CartPos = GetCartPos(ProductID)>
   
    <!--- Assuming the item was found, remove it from cart --->
    <CFIF CartPos GT 0>
      <CFSET This.CartArray = ArrayDeleteAt(This.CartArray, CartPos)>
    </CFIF>
  </CFFUNCTION>  
 
 
  <!--- *** EMPTY Method *** --->
  <CFFUNCTION
    NAME="Empty"
    HINT="Removes all items from the shopping cart">
   
    <!--- Empty the cart by clearing the This.CartArray array --->
    <CFSET ArrayClear(This.CartArray)>
  </CFFUNCTION>
 

  <!--- *** LIST Method *** --->
  <CFFUNCTION
    NAME="List"
    HINT="Returns a query object containing all items in shopping cart.  The query object has three columns: ProductID, Quantity and Price."
    RETURNTYPE="query">

    <!--- Create a query, to return to calling process --->
    <CFSET q = QueryNew("ProductID,Quantity,Price,Name,Dosage,Count")>
   
    <!--- For each item in cart, add row to query --->
    <CFLOOP FROM="1" TO="#ArrayLen(This.CartArray)#" INDEX="i">      ------- FIRST ERROR HERE ------------
      <CFSET QueryAddRow(q)>
      <CFSET QuerySetCell(q, "ProductID",  This.CartArray[i].ProductID)>
      <CFSET QuerySetCell(q, "Quantity", This.CartArray[i].Quantity)>
        <CFSET QuerySetCell(q, "Price", This.CartArray[i].Price)>
        <CFSET QuerySetCell(q, "Name", This.CartArray[i].Name)>
        <CFSET QuerySetCell(q, "Dosage", This.CartArray[i].Dosage)>
        <CFSET QuerySetCell(q, "Count", This.CartArray[i].Count)>
    </CFLOOP>

    <!--- Return completed query --->    
    <CFRETURN q>    
  </CFFUNCTION>  


  <!--- Internal GetCartPos() Method --->
  <CFFUNCTION
    NAME="GetCartPos"
    RETURNTYPE="numeric"
    ACCESS="private">
    <!--- Argument: ProductID --->
    <CFARGUMENT NAME="ProductID" TYPE="numeric" REQUIRED="Yes">
   
    <!--- Get position, if any, of ProductID in cart query --->
    <CFSET CurrentArrayPos = 0>
    <CFLOOP FROM="1" TO="#ArrayLen(This.CartArray)#" INDEX="i">           ---------  SECCOND ERROR HERE ----------------
      <CFIF This.CartArray[i].ProductID EQ Arguments.ProductID>
        <CFSET CurrentArrayPos = i>
        <CFBREAK>
      </CFIF>
    </CFLOOP>    
   
    <!--- Return the position --->
    <CFRETURN CurrentArrayPos>
  </CFFUNCTION>  


  <!--- Internal GetCartItem() Method --->
  <CFFUNCTION
    NAME="GetCartItem"
    RETURNTYPE="struct"
    ACCESS="private">
    <!--- One Argument: ProductID --->
    <CFARGUMENT NAME="ProductID" TYPE="numeric" REQUIRED="Yes">
   
    <!--- Get the position of the item in This.CartArray --->
    <CFSET CartPos = GetCartPos(ProductID)>
   
    <!--- If item for this ProductID was found, we will return it --->
    <CFIF CartPos GT 0>
      <CFSET CartItem = This.CartArray[CartPos]>
    <!--- If item was not found, create new one and add to cart --->  
    <CFELSE>
        
        <cfquery name="Pricing" dataSource="Cart">
                    SELECT *
                  FROM DrugDetails
                  WHERE DetailID = #Arguments.ProductID#
        </cfquery>      
   
        <CFSET CartItem = StructNew()>
      <CFSET CartItem.ProductID = Arguments.ProductID>
      <CFSET CartItem.Quantity = 0>
        <CFSET CartItem.Price = '#Pricing.Price#'>
        <CFSET CartItem.Name = '#Pricing.Name#'>
        <CFSET CartItem.Dosage = '#Pricing.Dosage#'>
        <CFSET CartItem.Count = '#Pricing.Count#'>
      <CFSET ArrayAppend(This.CartArray, CartItem)>

      </CFIF>

    <!--- In either case, return the item --->
    <CFRETURN CartItem>
  </CFFUNCTION>  
 
</CFCOMPONENT>
Avatar of mrichmon
mrichmon

I do not see any reason why those lines would be causing an error with quantity of 0 - ar you sure taht is where the error is?
Avatar of R_Hos

ASKER

yes.  wow.  its a relief to hear that i'm not the only one who cant figure out the problem
Avatar of R_Hos

ASKER

additionaly, something i just noticed is that the empty method is not working which makes me think that its something in how i'm either setting up the array or using it...
is it if you add an item of quantity 0 or if you remove items and the quantity ends up being 0 that the problem occurs?
Because this looks like the code from the CFWACK book and I know that that works since I used that as a starting point when writing my own shopping cart over - although that was almost 2 years ago when I wrote it, but it still works....
But technically you should never have a quantity of 0 since looking at the code the item should be removed here :

<!--- If the new quantity is greater than zero --->  
    <CFIF Quantity GT 0>
       <!--- Get structure that represents this item in cart, --->
       <!--- then set its quantity to the specified quantity --->
      <CFSET CartItem = GetCartItem(ProductID)>
      <CFSET CartItem.Quantity = Arguments.Quantity>
      <!--- If new quantity is zero, remove the item from cart --->
    <CFELSE>
      <CFSET This.Remove(ProductID)>
    </CFIF>


So I am not sure why a quantity of 0 would be causing this problem...
Do you have a sample URL where the problem is occuring?
Avatar of R_Hos

ASKER

that's exactly where i got the base code from and i've modified it.  I could swear that sometime in the past i had checked that changing the quantity to 0 just removed it from the cart... now it drops the error.

If you go to...

www.purduebooks.com/rxonline/frontend/productsearch.cfm 

you can do a search for products or see a list of products alphabeticly.  add something to the cart and then set the quantity to zero and click 'update quantities' and you can see my problem first hand.  

thanks
Okay interesting - it is not 0 that is causing the error.  I also tried a negative quantity and that caused the error - and once I got the error even going back and entering a different amount or a different product would give me the error....

Let me play a bit more...
Avatar of R_Hos

ASKER

once you get an error its stuck in the error until the session expires or till you delete the cookie.  i havnt seen the error with a positive number
ASKER CERTIFIED SOLUTION
Avatar of fmedia
fmedia

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of R_Hos

ASKER

sorry all, fell off ladder