Error in comparing a number!

hi there I have code where I have intervals.When I run the code I sometimes get this error.

" attempt to compare number with nil

stack traceback"

code is …

display.setStatusBar( display.HiddenStatusBar ) a = math.random( 60,1120) if a \> 1020 then b = math.random( 60, 800) print("B") elseif a \> 700 then b = math.random(60,480) print("BB") elseif a \> 380 then b = math.random( 60, 160) print("BBB") elseif a \< 160 then b = math.random(380, 1120) print("BBBB") end if ( a and b \> 160) and ((a \> 160) and( b \> 160)) then c = math.random( 60, 160) print("C") elseif ( a and b \< 1020) and (( a \< 1020) and ( b \< 1020) )then c = math.random(1020,1120) print("CC") elseif ( a and b \> 700 or a and b \< 800) and (( a \> 700 or a \< 800) and ( b \> 700 or b \< 800)) then c = math.random(700, 800) print("CCC") elseif( a and b \> 380 or a and b \< 480) and (( a \> 380 or a \< 480) and ( b \> 380 or b \< 480)) then c = math.random( 380, 480) print("CCCC") end print(a) print(b) print(c)

I think the composer is not getting any appropriate values to perform operations for code to print “CCC” and “CCCC”

Your code is a bit confusing, but I thing the problems are around not specifying explicitly what you are looking for in “a”.

The code “(if a or b > 160)” will translate to “if a is true or b > 160”, which is not what I think you want.  Also, the statements seem redundant.  “a” is not a boolean value here.

–john

Try re-design your code because in some cases variable b or c can be nil i.e. for 380 >= a >=160 you get b = nil. Maybe try set default values for a,b and c if is it possible.

Well I changed the code a bit… This code is working but not all the time.Sometimes it shows the same error.

display.setStatusBar( display.HiddenStatusBar ) a = math.random( 60,1120) if a \> 1020 then b = math.random( 60, 800) print("B") elseif a \> 700 then b = math.random(60,480) print("BB") elseif a \> 380 then b = math.random( 60, 160) print("BBB") elseif a \< 160 then b = math.random(380, 1120) print("BBBB") end if ( a \> 160) and ( b \> 160) then c = math.random(60,160) print("C") elseif( a \> 480 and b \> 480) or ( a \< 160 and b \> 480) or ( b \< 160 and a \> 480) then c = math.random( 380, 480) print("CC") elseif ( a \< 700 and b \< 700) or ( a \< 700 and b \> 800) or ( b \< 700 and b \> 800) then c = math.random( 700,800) print("CCC") elseif( a \< 1020) and ( b \< 1020) then c = math.random(1020, 1120) print("CCCC") end print(a) print(b) print(c)

yes you are right!working on it…

Give this a try:

display.setStatusBar( display.HiddenStatusBar ) local speed = 3 local function speedF() speed = speed + 1 print(speed) end Timer = timer.performWithDelay ( 15000, speedF , 0) local a local rectangleColors = {} rectangleColors[1] = {.5, 1, 1} rectangleColors[2] = {.3,.5,.2} rectangleColors[3] = {.3,.2,.8} a = math.random( 0,300) print(a) local myRectangle3 = display.newRect(280, 120, 30, 100 ) myRectangle3.strokeWidth = 3 myRectangle3:setFillColor( unpack(rectangleColors[1])) myRectangle3:setStrokeColor( 1, 0, 0 ) local myRectangle4 = display.newRect(280, -30, 30, 100 ) myRectangle4.strokeWidth = 3 myRectangle4:setFillColor( unpack(rectangleColors[2])) myRectangle4:setStrokeColor( 1, 0, 0 ) local myRectangle5 = display.newRect(280, -180, 30, 100 ) myRectangle5.strokeWidth = 3 myRectangle5:setFillColor( unpack(rectangleColors[3])) myRectangle5:setStrokeColor( 1, 0, 0 ) function moveBars(self, event) if self.y \> 670 then self.y = a self:setFillColor(unpack(rectangleColors[math.random(1,3)])) print("a") print ( self.y ) else self.y = self.y + speed end end myRectangle3.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle3) myRectangle4.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle4 ) myRectangle5.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle5 )

Your code is a bit confusing, but I thing the problems are around not specifying explicitly what you are looking for in “a”.

The code “(if a or b > 160)” will translate to “if a is true or b > 160”, which is not what I think you want.  Also, the statements seem redundant.  “a” is not a boolean value here.

–john

Try re-design your code because in some cases variable b or c can be nil i.e. for 380 >= a >=160 you get b = nil. Maybe try set default values for a,b and c if is it possible.

Well I changed the code a bit… This code is working but not all the time.Sometimes it shows the same error.

display.setStatusBar( display.HiddenStatusBar ) a = math.random( 60,1120) if a \> 1020 then b = math.random( 60, 800) print("B") elseif a \> 700 then b = math.random(60,480) print("BB") elseif a \> 380 then b = math.random( 60, 160) print("BBB") elseif a \< 160 then b = math.random(380, 1120) print("BBBB") end if ( a \> 160) and ( b \> 160) then c = math.random(60,160) print("C") elseif( a \> 480 and b \> 480) or ( a \< 160 and b \> 480) or ( b \< 160 and a \> 480) then c = math.random( 380, 480) print("CC") elseif ( a \< 700 and b \< 700) or ( a \< 700 and b \> 800) or ( b \< 700 and b \> 800) then c = math.random( 700,800) print("CCC") elseif( a \< 1020) and ( b \< 1020) then c = math.random(1020, 1120) print("CCCC") end print(a) print(b) print(c)

yes you are right!working on it…

Give this a try:

display.setStatusBar( display.HiddenStatusBar ) local speed = 3 local function speedF() speed = speed + 1 print(speed) end Timer = timer.performWithDelay ( 15000, speedF , 0) local a local rectangleColors = {} rectangleColors[1] = {.5, 1, 1} rectangleColors[2] = {.3,.5,.2} rectangleColors[3] = {.3,.2,.8} a = math.random( 0,300) print(a) local myRectangle3 = display.newRect(280, 120, 30, 100 ) myRectangle3.strokeWidth = 3 myRectangle3:setFillColor( unpack(rectangleColors[1])) myRectangle3:setStrokeColor( 1, 0, 0 ) local myRectangle4 = display.newRect(280, -30, 30, 100 ) myRectangle4.strokeWidth = 3 myRectangle4:setFillColor( unpack(rectangleColors[2])) myRectangle4:setStrokeColor( 1, 0, 0 ) local myRectangle5 = display.newRect(280, -180, 30, 100 ) myRectangle5.strokeWidth = 3 myRectangle5:setFillColor( unpack(rectangleColors[3])) myRectangle5:setStrokeColor( 1, 0, 0 ) function moveBars(self, event) if self.y \> 670 then self.y = a self:setFillColor(unpack(rectangleColors[math.random(1,3)])) print("a") print ( self.y ) else self.y = self.y + speed end end myRectangle3.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle3) myRectangle4.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle4 ) myRectangle5.enterFrame = moveBars Runtime:addEventListener("enterFrame",myRectangle5 )