CPB07
CPB07

Reputation: 699

ColdFusion - Array Element At Position X Cannot Be Found

I have the following script within a function that occasionally throws a "The element at position X cannot be found" error even though although the script will have called something using that element position earlier in the script. (e.g. I'll perhaps get an error relating to the element at position X not being found whilst calling the Player.getPlayerBIN function although it will have called the Player.getPlayerRecord function above it without any issue.

// IF 1 OR MORE RESULT RETURNED
if (LOCAL.numResults GT 0) {

    // LOOP ROUND RESULTS
    for (i=1; i <= LOCAL.numResults; i++) {

        // DEFAULT VARIABLES
        LOCAL.checkResourceID = '';
        LOCAL.insResource = '';
        LOCAL.getPlayerBIN = '';
        LOCAL.lowestBIN = 0;
        LOCAL.basePrice = 0;
        LOCAL.biddingPrice = 0;
        LOCAL.bidAmount = 0;
        LOCAL.binPrice = 0;
        LOCAL.startPrice = 0;
        LOCAL.timeLeft = 0;
        LOCAL.minsLeft = -1;
        LOCAL.auctionEnds = '';
        LOCAL.checkBids = '';
        LOCAL.insStuff = '';
        LOCAL.buyCard = '';
        LOCAL.bidCard = '';

        // CHECK IF RECORD EXISTS FOR RESOURCE ID
        LOCAL.checkResourceID = Application.cfcs.Player.getPlayerRecord(LOCAL.searchResults.auctionInfo[i].itemData.resourceID);
        if (LOCAL.checkResourceID.RecordCount EQ 0) {
                         LOCAL.insResource = Application.cfcs.Player.insPlayerResource(LOCAL.searchResults.auctionInfo[i].itemData.resourceID,
                         LOCAL.searchResults.auctionInfo[i].itemData.assetID,
                         LOCAL.searchResults.auctionInfo[i].itemData.rating,
                         LOCAL.searchResults.auctionInfo[i].itemData.rareFlag,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[1].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[2].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[3].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[4].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[5].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[6].value);   
        }

        // QUERY PLAYER BIN PRICE DATA
        LOCAL.getPlayerBIN = Application.cfcs.Player.getPlayerBIN(LOCAL.searchResults.auctionInfo[i].itemData.resourceID);

        // IF LOWEST BIN RECORD EXISTS
        if (LOCAL.getPlayerBIN.RecordCount GT 0) {

            // SET LATEST LOWEST BIN PRICE
            LOCAL.lowestBIN = LOCAL.getPlayerBIN.lowestBIN;

            // SET BUYING & SELLING PRICES
            //if (LOCAL.lowestBIN GT 3000) {
            //  LOCAL.basePrice = LOCAL.lowestBIN * 0.9;
            //} else {
                LOCAL.basePrice = LOCAL.lowestBIN * 0.8;
            //}
            if (LOCAL.lowestBIN LT 1000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 50) * 50;
                LOCAL.binPrice = LOCAL.lowestBIN - 50;
                LOCAL.startPrice = LOCAL.binPrice - 50;
            } else if (LOCAL.lowestBIN LT 10000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 100) * 100;
                LOCAL.binPrice = LOCAL.lowestBIN - 100;
                LOCAL.startPrice = LOCAL.binPrice - 100;
            } else if (LOCAL.lowestBIN LT 50000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 250) * 250;
                LOCAL.binPrice = LOCAL.lowestBIN - 250;
                LOCAL.startPrice = LOCAL.binPrice - 250;
            } else if (LOCAL.lowestBIN LT 100000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 500) * 500;
                LOCAL.binPrice = LOCAL.lowestBIN - 500;
                LOCAL.startPrice = LOCAL.binPrice - 500;
            } else if (LOCAL.lowestBIN GTE 100000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 1000) * 1000;
                LOCAL.binPrice = LOCAL.lowestBIN - 1000;
                LOCAL.startPrice = LOCAL.binPrice - 1000;
            }
        }

        // IF BIN PRICE SET AND IS LESS THAN SET BUYING PRICE
        if (LOCAL.searchResults.auctionInfo[i].buyNowPrice GT 0 AND LOCAL.searchResults.auctionInfo[i].buyNowPrice LTE LOCAL.biddingPrice) {
            LOCAL.timeLeft = LOCAL.searchResults.auctionInfo[i].expires;
            LOCAL.auctionEnds = DateAdd("s",LOCAL.timeLeft,Now());
            LOCAL.insStuff = Application.cfcs.Bid.insStuff(
                               LOCAL.resourceID,
                               LOCAL.searchResults.auctionInfo[i].itemData.resourceID,
                               LOCAL.getPlayerBIN.resourceID,
                               LOCAL.searchResults.auctionInfo[i].itemData.preferredPosition,
                               LOCAL.searchResults.auctionInfo[i].itemData.playStyle,
                               LOCAL.lowestBIN,
                               LOCAL.searchResults.auctionInfo[i].startingBid,
                               LOCAL.searchResults.auctionInfo[i].buyNowPrice,
                               LOCAL.searchResults.auctionInfo[i].currentBid,
                               LOCAL.basePrice,
                               LOCAL.biddingPrice,
                               LOCAL.bidAmount,
                               LOCAL.startPrice,
                               LOCAL.binPrice);

        // ELSE IF LISTING START PRICE & CURRENT BID LESS THAN BIDDING PRICE
        } else if (LOCAL.searchResults.auctionInfo[i].startingBid LT LOCAL.biddingPrice AND LOCAL.searchResults.auctionInfo[i].currentBid LT LOCAL.biddingPrice) {
            if (LOCAL.searchResults.auctionInfo[i].currentBid LT LOCAL.searchResults.auctionInfo[i].startingBid) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].startingBid; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid LT 1000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 50; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid LT 10000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 100; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid LT 50000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 250; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid LT 100000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 500; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid GTE 100000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 1000; }
            LOCAL.timeLeft = LOCAL.searchResults.auctionInfo[i].expires;
            LOCAL.auctionEnds = DateAdd("s",LOCAL.timeLeft,Now());
            LOCAL.minsLeft = DateDiff("n",now(),LOCAL.auctionEnds);
            if (LOCAL.minsLeft LTE 25 AND LOCAL.bidAmount LT LOCAL.lowestBIN) {
                LOCAL.checkBids = Application.cfcs.Bid.getBids(LOCAL.searchResults.auctionInfo[i].itemData.ID);
                if (LOCAL.checkBids.RecordCount EQ 0) {
                    LOCAL.insStuff = Application.cfcs.Bid.insStuff(
                                               LOCAL.resourceID,
                                             LOCAL.searchResults.auctionInfo[i].itemData.resourceID,
                                               LOCAL.getPlayerBIN.resourceID,
                                           LOCAL.searchResults.auctionInfo[i].itemData.preferredPosition,
                                               LOCAL.searchResults.auctionInfo[i].itemData.playStyle,
                                               LOCAL.lowestBIN,
                                               LOCAL.searchResults.auctionInfo[i].startingBid,
                                               LOCAL.searchResults.auctionInfo[i].buyNowPrice,
                                               LOCAL.searchResults.auctionInfo[i].currentBid,
                                               LOCAL.basePrice,
                                               LOCAL.biddingPrice,
                                               LOCAL.bidAmount,
                                               LOCAL.startPrice,
                                               LOCAL.binPrice);

                }
            }
        }
    }
}

Anyone got any ideas or advice about what might be going as I couldn't find very much on a google search :(

Thanks

Upvotes: 0

Views: 3802

Answers (2)

Adam Cameron
Adam Cameron

Reputation: 29870

I dunno why Peter didn't make this an answer, so I will.

The problem is almost certainly because you are not VAR-scoping your loop counter variable i. If the CFC instance this is in is in a shared scope, then all requests using that function will be sharing / changing the same i variable simultaneously, which will destabilise your code along the lines of what you're seeing.

Always VAR your function-local variables, or specify the local scope when referencing them. All the time.

Upvotes: 3

James A Mohler
James A Mohler

Reputation: 11120

LOCAL.numResults is really not needed

Suggestion 1

Use arraylen

for (i=1; i <= ArrayLen(LOCAL.searchResults.auctionInfo[i]); i++) {
   ...
   } 

This only works if your array goes 1 to ArrayLen()

Suggestion 2

Use for-in

for (local.myAuctionInfo in LOCAL.searchResults.auctionInfo) {
   ...
   }

See For in loop

This will work even if your array does not have data in all its indexes

Upvotes: 0

Related Questions