Function not getting data from text field

I made what I thought was a simple function to grab two numbers from text fields and multiply them together.

It did not write a result to the target field, so I reduced it to this:

[lua] local function CalcFunction(event)

VolFactor=getText(Volumefactor)
print (VolFactor)

end[/lua]

Basically, I just wanted to grab the text value and repeat it in the terminal window so I can see it has some data. When I click the calculate button in the simulator, nothing happens. No results show up in the terminal window. 

I used to have the function near the top of my code, under some variable definitions, but before the creation of the text fields. It crashed and I assumed that was because it was trying to access a text field (Volumefactor) that did not exist at that point (Error said value of Volumefactor was nil).

So i moved it to the end of the code, after all the field definitions. Now the app comes up in the simulator, but it doesn’t do anything.

I’m not sure why the variable VolFactor is not getting anything from the text field.

would need to see more code otherwise we would be just guessing

[lua]
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require(“widget”);
local myapp = require(“myapp”)

–variables for calculate function
local Area = 0
local Thick = 0
local Rel = 0
local NG = 0
local Por = 0
local Sat = 0
local VolFactor = 0
local FFactor = 0
local CalcRelief = 0

– Called when the scene’s view does not exist:
function scene:createScene( event )

local function onRelief (event)
storyboard.showOverlay( “relief_select”, {
effect = “slideLeft”,
time = 500,
isModal = true})
return true;
end

local function onOilType (event)
storyboard.showOverlay( “oil_select”, {
effect = “slideLeft”,
time = 500,
isModal = true})
return true;
end

local function onHelp (event)
storyboard.showOverlay( “help_screen”, {
effect = “slideLeft”,
time = 500,
isModal = true})
return true;
end

local sceneView = self.view

local bg = display.newRect(sceneView, 0,0, display.contentWidth, display.contentHeight-display.statusBarHeight )
bg.anchorX, bg.anchorY = 0,0
bg:setFillColor(.5, .5, .5)
bg:addEventListener(“touch”,function (event) native.setKeyboardFocus(nil) end )

local editGroup = display.newGroup()
sceneView:insert(editGroup)

–title bar
local titlebar = display.newRect( editGroup, 0,10, 2*display.contentWidth, 25)
titlebar:setFillColor( 0,0,0 ) --black
local apptitle = display.newText( editGroup, “Volume*”, 50, 12, “Arial”, 24 )
apptitle:setFillColor( 255, 255, 255 )

–data fields
local AreaGRV = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.09,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“km2/km3”,
}

editGroup:insert( AreaGRV )

local AreaLabel = display.newText( editGroup, “Area/GRV”, display.contentWidth*.4, display.contentHeight*.09, “Arial”, 14 )
AreaLabel:setFillColor( 0,0,0,1 )

local Thickness = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.16,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“m”
}

editGroup:insert( Thickness )

local ThickLabel = display.newText( editGroup, “Thickness”, display.contentWidth*.39, display.contentHeight*.16, “Arial”, 14 )
ThickLabel:setFillColor( 0,0,0,1 )

local Blanklabel = display.newText( editGroup, “(Blank if using GRV)”, display.contentWidth*.61, display.contentHeight*.165, “Arial”, 8 )
Blanklabel:setFillColor( 0,0,0,1 )

local Relief = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.23,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“m or frac”,
onRelease=onRelief
}

editGroup:insert( Relief )

local ReliefLabel = display.newText( editGroup, “Relief”, display.contentWidth*.355, display.contentHeight*.23, “Arial”, 14 )
ReliefLabel:setFillColor( 0,0,0,1 )

local ReliefButton = widget.newButton(
{
x = display.contentWidth *.75,
y = display.contentHeight*.23,
width = 90,
height = 30,
onRelease = onRelief,
label = “Select Relief”,
labelColor={default ={1,1,1}, over={0,0,0}}
})

editGroup:insert( ReliefButton )

local NetGross = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.3,
width=display.contentWidth*.26,
inputType=“number”,
placeholder= “frac or %”
}

editGroup:insert( NetGross )

local NetGrossLabel = display.newText( editGroup, “Net:Gross”, display.contentWidth*.39, display.contentHeight*.3, “Arial”, 14 )
NetGrossLabel:setFillColor( 0,0,0,1 )

local Porosity = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.37,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“pu or %”
}

editGroup:insert( Porosity )

local PorosityLabel = display.newText( editGroup, “Porosity”, display.contentWidth*.375, display.contentHeight*.37, “Arial”, 14 )
PorosityLabel:setFillColor( 0,0,0,1 )

local Saturation = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.44,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“frac or %”
}

editGroup:insert( Saturation )

local SaturationLabel = display.newText( editGroup, “Saturation”, display.contentWidth*.39, display.contentHeight*.44, “Arial”, 14 )
SaturationLabel:setFillColor( 0,0,0,1 )

Volumefactor = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.51,
width=display.contentWidth*.26,
inputType=“number”,
text=“test”
}

editGroup:insert( Volumefactor )

local VolumeLabel = display.newText( editGroup, “Volume factor”, display.contentWidth*.42, display.contentHeight*.51, “Arial”, 14 )
VolumeLabel:setFillColor( 0,0,0,1 )

local OilSelectButton = widget.newButton(
{
x = display.contentWidth *.75,
y = display.contentHeight*.51,
width = 90,
height = 30,
onRelease = onOilType,
label = “Oil Type”,
labelColor={default ={1,1,1}, over={0,0,0}}
})

editGroup:insert( OilSelectButton )

local FudgeFactor = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.58,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“arbitrary”
}

editGroup:insert( FudgeFactor )

local FudgeLabel = display.newText( editGroup, “Fudge factor”, display.contentWidth*.42, display.contentHeight*.58, “Arial”, 14 )
FudgeLabel:setFillColor( 0,0,0,1 )

local CalcButton = widget.newButton(
{
x = display.contentWidth *.75,
y = display.contentHeight*.65,
width = 90,
height = 30,
onRelease = CalcFunction,
label = “Calculate”,
labelColor={default ={1,0,0}, over={0,0,0}},
font = “Arial Bold”
})

editGroup:insert( CalcButton )

local dividerline = display.newLine( editGroup, 0, display.contentHeight*.615, display.contentWidth, display.contentHeight*.615 )
dividerline:setStrokeColor( 0,0,0,1 )
dividerline.strokeWidth=3

–result fields
local CalcResultbbl = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.65,
width=display.contentWidth*.26,
inputType=“number”
}

editGroup:insert( CalcResultbbl )

local FudgeLabel = display.newText( editGroup, “Million bbl”, display.contentWidth*.39, display.contentHeight*.65, “Arial”, 14 )
FudgeLabel:setFillColor( 0,0,0,1 )

local CalcResultm3 = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.72,
width=display.contentWidth*.26,
inputType=“number”,
}

editGroup:insert( CalcResultm3 )

local FudgeLabel = display.newText( editGroup, “Million m3”, display.contentWidth*.39, display.contentHeight*.72, “Arial”, 14 )
FudgeLabel:setFillColor( 0,0,0,1 )

local function CalcFunction(event)

–Area=AreaGRV:getText()

–or VolFactor=VolumeFactor:getText()?
VolFactor=getText(Volumefactor)
print (VolFactor)

–local AreaNumber = tonumber( Area )
–print(AreaNumber)

–[[ local VolNumber = tonumber( VolFactor )

local Resultbbl = 1 * VolNumber
print(VolNumber)
print(Resultbbl)]]
CalcResultbbl:setText(Resultbbl)
–return true
end
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )

end

– Called when scene is about to move offscreen:
function scene:exitScene( event )

end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene[/lua]

Hi Sean,

You need to forward declare the function above the creation of the button itself

for example add somewhere at the top

local CalcFunction

and below where you implement the function itself:

CalcFunction = function(event)

end

Once I realized that I had to implement the CalcFunction outside of the function scene:createScene, it worked.

Thanks for the help.

would need to see more code otherwise we would be just guessing

[lua]
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require(“widget”);
local myapp = require(“myapp”)

–variables for calculate function
local Area = 0
local Thick = 0
local Rel = 0
local NG = 0
local Por = 0
local Sat = 0
local VolFactor = 0
local FFactor = 0
local CalcRelief = 0

– Called when the scene’s view does not exist:
function scene:createScene( event )

local function onRelief (event)
storyboard.showOverlay( “relief_select”, {
effect = “slideLeft”,
time = 500,
isModal = true})
return true;
end

local function onOilType (event)
storyboard.showOverlay( “oil_select”, {
effect = “slideLeft”,
time = 500,
isModal = true})
return true;
end

local function onHelp (event)
storyboard.showOverlay( “help_screen”, {
effect = “slideLeft”,
time = 500,
isModal = true})
return true;
end

local sceneView = self.view

local bg = display.newRect(sceneView, 0,0, display.contentWidth, display.contentHeight-display.statusBarHeight )
bg.anchorX, bg.anchorY = 0,0
bg:setFillColor(.5, .5, .5)
bg:addEventListener(“touch”,function (event) native.setKeyboardFocus(nil) end )

local editGroup = display.newGroup()
sceneView:insert(editGroup)

–title bar
local titlebar = display.newRect( editGroup, 0,10, 2*display.contentWidth, 25)
titlebar:setFillColor( 0,0,0 ) --black
local apptitle = display.newText( editGroup, “Volume*”, 50, 12, “Arial”, 24 )
apptitle:setFillColor( 255, 255, 255 )

–data fields
local AreaGRV = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.09,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“km2/km3”,
}

editGroup:insert( AreaGRV )

local AreaLabel = display.newText( editGroup, “Area/GRV”, display.contentWidth*.4, display.contentHeight*.09, “Arial”, 14 )
AreaLabel:setFillColor( 0,0,0,1 )

local Thickness = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.16,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“m”
}

editGroup:insert( Thickness )

local ThickLabel = display.newText( editGroup, “Thickness”, display.contentWidth*.39, display.contentHeight*.16, “Arial”, 14 )
ThickLabel:setFillColor( 0,0,0,1 )

local Blanklabel = display.newText( editGroup, “(Blank if using GRV)”, display.contentWidth*.61, display.contentHeight*.165, “Arial”, 8 )
Blanklabel:setFillColor( 0,0,0,1 )

local Relief = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.23,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“m or frac”,
onRelease=onRelief
}

editGroup:insert( Relief )

local ReliefLabel = display.newText( editGroup, “Relief”, display.contentWidth*.355, display.contentHeight*.23, “Arial”, 14 )
ReliefLabel:setFillColor( 0,0,0,1 )

local ReliefButton = widget.newButton(
{
x = display.contentWidth *.75,
y = display.contentHeight*.23,
width = 90,
height = 30,
onRelease = onRelief,
label = “Select Relief”,
labelColor={default ={1,1,1}, over={0,0,0}}
})

editGroup:insert( ReliefButton )

local NetGross = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.3,
width=display.contentWidth*.26,
inputType=“number”,
placeholder= “frac or %”
}

editGroup:insert( NetGross )

local NetGrossLabel = display.newText( editGroup, “Net:Gross”, display.contentWidth*.39, display.contentHeight*.3, “Arial”, 14 )
NetGrossLabel:setFillColor( 0,0,0,1 )

local Porosity = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.37,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“pu or %”
}

editGroup:insert( Porosity )

local PorosityLabel = display.newText( editGroup, “Porosity”, display.contentWidth*.375, display.contentHeight*.37, “Arial”, 14 )
PorosityLabel:setFillColor( 0,0,0,1 )

local Saturation = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.44,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“frac or %”
}

editGroup:insert( Saturation )

local SaturationLabel = display.newText( editGroup, “Saturation”, display.contentWidth*.39, display.contentHeight*.44, “Arial”, 14 )
SaturationLabel:setFillColor( 0,0,0,1 )

Volumefactor = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.51,
width=display.contentWidth*.26,
inputType=“number”,
text=“test”
}

editGroup:insert( Volumefactor )

local VolumeLabel = display.newText( editGroup, “Volume factor”, display.contentWidth*.42, display.contentHeight*.51, “Arial”, 14 )
VolumeLabel:setFillColor( 0,0,0,1 )

local OilSelectButton = widget.newButton(
{
x = display.contentWidth *.75,
y = display.contentHeight*.51,
width = 90,
height = 30,
onRelease = onOilType,
label = “Oil Type”,
labelColor={default ={1,1,1}, over={0,0,0}}
})

editGroup:insert( OilSelectButton )

local FudgeFactor = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.58,
width=display.contentWidth*.26,
inputType=“number”,
placeholder=“arbitrary”
}

editGroup:insert( FudgeFactor )

local FudgeLabel = display.newText( editGroup, “Fudge factor”, display.contentWidth*.42, display.contentHeight*.58, “Arial”, 14 )
FudgeLabel:setFillColor( 0,0,0,1 )

local CalcButton = widget.newButton(
{
x = display.contentWidth *.75,
y = display.contentHeight*.65,
width = 90,
height = 30,
onRelease = CalcFunction,
label = “Calculate”,
labelColor={default ={1,0,0}, over={0,0,0}},
font = “Arial Bold”
})

editGroup:insert( CalcButton )

local dividerline = display.newLine( editGroup, 0, display.contentHeight*.615, display.contentWidth, display.contentHeight*.615 )
dividerline:setStrokeColor( 0,0,0,1 )
dividerline.strokeWidth=3

–result fields
local CalcResultbbl = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.65,
width=display.contentWidth*.26,
inputType=“number”
}

editGroup:insert( CalcResultbbl )

local FudgeLabel = display.newText( editGroup, “Million bbl”, display.contentWidth*.39, display.contentHeight*.65, “Arial”, 14 )
FudgeLabel:setFillColor( 0,0,0,1 )

local CalcResultm3 = widget.newEditField
{
slideGroup=editGroup,
x=display.contentWidth*.14,
y=display.contentHeight*.72,
width=display.contentWidth*.26,
inputType=“number”,
}

editGroup:insert( CalcResultm3 )

local FudgeLabel = display.newText( editGroup, “Million m3”, display.contentWidth*.39, display.contentHeight*.72, “Arial”, 14 )
FudgeLabel:setFillColor( 0,0,0,1 )

local function CalcFunction(event)

–Area=AreaGRV:getText()

–or VolFactor=VolumeFactor:getText()?
VolFactor=getText(Volumefactor)
print (VolFactor)

–local AreaNumber = tonumber( Area )
–print(AreaNumber)

–[[ local VolNumber = tonumber( VolFactor )

local Resultbbl = 1 * VolNumber
print(VolNumber)
print(Resultbbl)]]
CalcResultbbl:setText(Resultbbl)
–return true
end
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )

end

– Called when scene is about to move offscreen:
function scene:exitScene( event )

end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene[/lua]

Hi Sean,

You need to forward declare the function above the creation of the button itself

for example add somewhere at the top

local CalcFunction

and below where you implement the function itself:

CalcFunction = function(event)

end

Once I realized that I had to implement the CalcFunction outside of the function scene:createScene, it worked.

Thanks for the help.