Graphics rescaling crash

Hello, I am getting a crash after trying to rescale an image in my code. 

print("Hello there!") local fillObj = display.newImage( "hello.png", display.contentCenterX, display.contentCenterY ) local xViewSize = display.viewableContentWidth local yViewSize = display.viewableContentHeight local xFill = xViewSize / fillObj.width local yFill = yViewSize / fillObj.height print("Width") print(display.viewableContentWidth) print(xFill) print("Height") print(display.viewableContentHeight) print(yFill) print("--------") local scaleObj = display.newImage( "drink.png", display.contentCenterX, display.contentCenterY ) if display.viewableContentWidth \> scaleObj.width then local xScale = display.viewableContentWidth / scaleObj.width else local xScale = display.viewableContentWidth / scaleObj.height end if display.viewableContentHeight \> scaleObj.height then local yScale = display.viewableContentHeight / scaleObj.height else local yScale = display.viewableContentHeight / scaleObj.width end print(xScale) print(yScale) local txt = display.newText( "Hello", display.contentCenterX, display.contentCenterY / 2, native.systemFont, 50 ) scaleObj:setFillColor( 0, 0.4, 0 ) ---- fillObj:scale(xFill,yFill) scaleObj:scale(xScale,yScale)

Does anyone know what the crash is being caused by?

EDIT: xScale and yScale is ‘nil’. Why?

Here is the crash log.

2013-12-01 11:45:13.587 Corona Simulator[2139:d03] Hello there! 2013-12-01 11:45:13.624 Corona Simulator[2139:d03] Width 2013-12-01 11:45:13.624 Corona Simulator[2139:d03] 320 2013-12-01 11:45:13.625 Corona Simulator[2139:d03] 0.93841642228739 2013-12-01 11:45:13.625 Corona Simulator[2139:d03] Height 2013-12-01 11:45:13.625 Corona Simulator[2139:d03] 480 2013-12-01 11:45:13.625 Corona Simulator[2139:d03] 0.9375 2013-12-01 11:45:13.625 Corona Simulator[2139:d03] -------- 2013-12-01 11:45:13.649 Corona Simulator[2139:d03] nil 2013-12-01 11:45:13.650 Corona Simulator[2139:d03] nil 2013-12-01 11:45:13.651 Corona Simulator[2139:d03] Runtime error /Users/eric/Documents/Lua/CoronaSDK/testing/main.lua:53: bad argument #1 to 'scale' (number expected, got nil) stack traceback: [C]: ? [C]: in function 'scale' /Users/eric/Documents/Lua/CoronaSDK/testing/main.lua:53: in main chunk

You are creating local variables for xScale and yScale inside the if - then - end blocks of your code.  Outside those blocks xScale and yScale are undefined, and thus nil.  The solution is to first create the variables outside those code blocks, near the top of your code:

local xScale local yScale  

They’ll still be nil since you haven’t given them any values yet.  Then, in the if-then statements, remove “local” before each xScale and yScale.  Now you will be creating values for the variables you predefined above. ( If you left the local declarations inside the if-then blocks you’d be creating different variables with the same name but that are “local” to the scope of the if-then block, and the variables you really want to change would still be nil)

You are creating local variables for xScale and yScale inside the if - then - end blocks of your code.  Outside those blocks xScale and yScale are undefined, and thus nil.  The solution is to first create the variables outside those code blocks, near the top of your code:

local xScale local yScale  

They’ll still be nil since you haven’t given them any values yet.  Then, in the if-then statements, remove “local” before each xScale and yScale.  Now you will be creating values for the variables you predefined above. ( If you left the local declarations inside the if-then blocks you’d be creating different variables with the same name but that are “local” to the scope of the if-then block, and the variables you really want to change would still be nil)