[Resolved] Random 'nil' error with PerformWithDelay?

As a newbie, I’m experimenting with dealing a deck of cards from left to right. All usually works well, but randomly the following error appears, any help would be appreciated:

Runtime error
c:\dev\corona\vpu\job.lua:216: attempt to concatenate local ‘nslot’ (a n
il value)
stack traceback:
[C]: ?
c:\dev\corona\vpu\job.lua:216: in function ‘_listener’
?: in function <?:514>
?: in function <?:215>
Here is the code:

function initialDeal()

 function showCard(event)
 audio.play(cardsound);
 local nindex = event.source.params[1];
 local nslot = event.source.params[2];

 local x = bfv[nindex].x;
 local y = bfv[nindex].y;
 bfv[nindex] = display.newImage("images/"..nslot..".png");
 bfv[nindex]:scale(1.2,1.2);
 bfv[nindex].x = x;
 bfv[nindex].y = y;
 timer.cancel(cardtimers[nindex]);

 end

 local seed = os.time();
 math.randomseed(seed);

 cards = cardfunctions.shuffleDeck();
 for i=1, 5 do
 cardtimers[i] = timer.performWithDelay(200\*i,showCard);
 local params = {}
 params[1] = i;

 local cardno = math.random(1,54);
 while (cards[cardno] == -1) do
 cardno = math.random(1,54);
 end
 params[2] = cards[cardno];
 cards[cardno] = -1;
 cardtimers[i].params = params;
 end

end

[import]uid: 66769 topic_id: 25394 reply_id: 325394[/import]

This looks fairly advanced for a newbie, good for you :slight_smile:

That error is stating that;
local nslot = event.source.params[2]
is nil.

So, one way to debug would be to do;
print (nslot)
right after that line, it will likely show nil - if it doesn’t then you know the error is elsewhere but if does then you know that local nslot = event.source.params[2] is nil also.

One other thing I always try it removing the “local” when I have issues like this and though it doesn’t always help it can at times so may be worth making a note about it. (I used to always trip up on that.)

Let me know how you go with that :slight_smile:

Peach [import]uid: 52491 topic_id: 25394 reply_id: 102647[/import]

Thanks. The error only happens **very** randomly, which makes it very frustrating!!

I believe the timing gets off and the line:

cardtimers[i] = timer.performWithDelay(200*i,showCard);

is firing before the parameters below it get set. [import]uid: 66769 topic_id: 25394 reply_id: 102652[/import]

To isolate this further perhaps you might try lowering the delay to almost nothing and then bumping it up to 1000.

If it is indeed the issue the problem should happen far more frequently at the lower end.

Would be interested to hear results and confirm whether or not this is indeed the cause :slight_smile: [import]uid: 52491 topic_id: 25394 reply_id: 102759[/import]

It turns out there’s nothing wrong with ‘PerformWithDelay’ at all, but the array line

params[2] = cards[cardno]

was actually returning a null value, so the error was real.

Chalk it up to a newbie mistake.
[import]uid: 66769 topic_id: 25394 reply_id: 102765[/import]

These things happen sometimes, newbie or not.

Well done on getting it sorted, marking as resolved :slight_smile: [import]uid: 52491 topic_id: 25394 reply_id: 102841[/import]