简体   繁体   中英

Global variables have nil value, attempt to call global (a nil value) in Corona

I attempt to get data (in a global variable) from user through a textbox from the user in functions, and use these variables to insert into a sqlite table.

But, when I attempt to do so, I get an error like "attempt to call global locationGlobal (a nil value)" in the INSERT INTO line, after I click the submit button.

NOTE : I input values in the textbox by using Bluestacks android simulator as inputting the text is not possible in windows Corona SDK. Here is my code :

local widget = require("widget")
require "sqlite3"   
local path = system.pathForFile("testUser.db", system.DocumentsDirectory)
db = sqlite3.open( path )  

--local location,area,arrivalTime,departTime,eventAttended 

local function onSystemEvent( event )
        if( event.type == "applicationExit" ) then              
            db:close()
        end
end

--local tablesetup = [[CREATE TABLE IF NOT EXISTS visitPlace (id INTEGER PRIMARY KEY autoincrement, location, area, arrivalTime, departTime, eventAttended);]]
local tablesetup = [[CREATE TABLE place (id INTEGER PRIMARY KEY autoincrement, location);]]
print(tablesetup)
db:exec( tablesetup )

_W = display.viewableContentWidth
_H = display.viewableContentHeight

local background = display.newRect(0,0,_W,_H)
background:setFillColor(0,0,0)    


local function textListenerLocation( event )

    if ( event.phase == "began" ) then
        -- user begins editing defaultField
        event.target.text = ''        
        print(location)            
        print( event.text )

    elseif ( event.phase == "ended" ) then
        -- do something with defaultField text          
         locationGlobal = tostring( event.target.text)             

    elseif ( event.phase == "editing" ) then
        print( event.newCharacters )
        print( event.oldText )
        print( event.startPosition )
        print( event.text )


    elseif ( event.phase == "submitted" ) then
        locationGlobal =tostring( event.target.text)            
        --local label = display.newText( location, 180, 30, native.systemFontBold, 20 )
       -- label:setFillColor( 190/255, 190/255, 1 )           
    end
end

local function SubmitEvent( event )
    --local label = display.newText( location, 180, 30, native.systemFontBold, 20 )
   --label:setFillColor( 190/255, 190/255, 1 )


    local insertionTable = [[INSERT INTO visitPlace VALUES(NULL,']]..locationGlobal..[[) ]]
    db:exec(insertionTable)

    for row in db:nrows("SELECT * FROM visitPlace") do
        local text = row.location
        local t = display.newText(text, 450, 120*row.id, native.systemFont, 40)
        t:setFillColor(1,0,1)

    end 
   local label1 = display.newText( "Submitted", 180, 30, native.systemFontBold, 20 )
   label1:setFillColor( 190/255, 190/255, 1 )
end

function background:tap( event )
    native.setKeyboardFocus(nil)
end



local locationTxtbox = native.newTextField(180,140,280,100)
locationTxtbox.size = 34
locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)


local submitButton =  widget.newButton
{
    label = "Submit",
    onEvent = SubmitEvent,
    shape = "roundedRect",
    width = 100,
    height = 30,
    cornerRadius = 2    
}

submitButton.x = display.contentCenterX + (display.contentCenterX/2)
submitButton.Y = display.contentCenterY + (display.contentCenterY/2)

background:addEventListener("tap",background)
Runtime:addEventListener( "system", onSystemEvent )

--defaultField = native.newTextField( 150, 150, 180, 30 )

--defaultField:addEventListener( "userInput", textListener )

currently your textlistenerlocation function is not being called. So when you hit the submit button, locationGlobal has not yet been defined.

The following looks suspicious:

locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)

the addEventListener should take an event string and then a function to call when the event happens. textListenerLocation is your event handling function, it is not the event string.

locationTxtbox:addEventListener("userInput", textListenerLocation)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM