Why do i get an error when i try to add another widget button in the createScene(event) section?

Hi, im new to corona. I’m getting an error when i try to add another button to the createScene(event). anyone can help me?

this is the error;

?:0: attempt to index nil value

stack traceback:

?:in function"?’

?:in function <?:1259>

(tail call:?

?:in function<?:122>

(tail call): ?

?:in function ‘dispatchEvent’

?:in function ‘gotoScene’

and this is my Code for it

function scene:createScene(event)
        screenGroup = self.view
        
        background = display.newImage(“Images/Background.png”);
        background.x = display.contentCenterX;
        background.y = display.contentCenterY;

        screenGroup:insert(background)
    
    local StartButton = widget.newButton
    {
        left = 230,
        top = 235,
        defaultFile = “Buttons/Start.png”,
        overFile = “Buttons/StartOff.png”,
        onEvent = StartButtonEventHandler,
    }

    StartButton:addEventListener(“tap”,StartButton)

    local OptionsButton = widget.newButton
    {
    left = 80,
    top = 80,
    defaultFile = “Buttons/Options.png”,
    overFile = “Button/OptionsOff.png”,
    onEvent = OptionsButtonEventHandler,
    }

    OptionsButton:addEventListener(“tap”,OptionsButton)

    buttonGroup:insert(StartButton)
    buttonGroup:insert(OptionsButton)
 

Maybe add the event listener last

Why are you adding these Event Listeners? You usually don’t need to add these with widgets. When the button is touched, the function name you have provided with the onEvent option will be called. Do you have these functions defined? If not the code will try to call a function that does not exist and you’ll get cryptic error messages like the one you’re getting.

I agree with @elbowroomapps.

Remove all the addEventListener calls and I think you’ll be fine.

@elbowroomapps

I’ve tried removing the addEventListener of both in start and in options, but still i get the same error. but if i remove the Options button code, it runs well, no error or anything in the start button that i have.

In your code for the Options button you have:

overFile = "Button/OptionsOff.png",

…shouldn’t that be " Buttons /OptionsOff.png" ?

Sir @ingemar

I changed it but still getting the same error. also whats confusing me is when i try to code to another file with 2 buttons like in my “scene1” lua file

this is the code;

local storyboard = require(“storyboard”);
local widget = require(“widget”);
local scene = storyboard.newScene();
local screenGroup;
local buttonGroup = display.newGroup();

    local function NextButtonEventHandler(event)
        local phase = event.phase

        if event.phase == “ended” then
            print(“next button has been pressed”)
            storyboard.gotoScene(“scene2”, “fade”, 100)
            return true
        end
    end

    local function BackButtonEventHandler(event)
        local phase = event.phase

        if event.phase == “ended” then
            print(“back button has been pressed”)
            storyboard.gotoScene(“menu”, “fade”, 100)
            return true
        end
    end

function scene:createScene(event)
        screenGroup = self.view
        
        local background = display.newImage(“Images/First1.png”);
        background.x = display.contentCenterX;
        background.y = display.contentCenterY;

        screenGroup:insert(background)

    local NextButton = widget.newButton(
        left = -150,
        top = 600,
        width = 210,
        height = 150,
        defaultFile = “Buttons/Next.png”,
        overFile = “Buttons/NextOff.png”,
        onEvent = NextButtonEventHandler,
    }
    
    
    local BackButton = widget.newButton
    {
    left = 950,
    top = 600,
    width = 210,
    height = 150,
    defaultFile = “Buttons/Back.png”,
    overFile = “Buttons/BackOff.png”,
    onEvent = BackButtonEventHandler,

}

    buttonGroup:insert(NextButton)
    buttonGroup:insert(BackButton)
end

function scene:enterScene(event)

    
end

function scene:exitScene(event)
    buttonGroup:removeSelf()
    buttonGroup = nil
    screenGroup:removeSelf()
    screenGroup = nil

end

function scene:destroyScene(event)
end
        

scene:addEventListener(“createScene”);
scene:addEventListener(“enterScene”);
scene:addEventListener(“exitScene”);
scene:addEventListener(“destroyScene”);

return scene;

i don’t get the error i have been getting at my “menu” lua file. despite having 2 buttons for the “scene1” file. i’ve tried re-coding my “menu” lua file but still getting the name error for it.

mistyped my last sentence “getting the name error” i meant “getting the same error”

When do you get the error?

Immediately when you run the app, or when you press a button?

Immediately when i run the app

Is your menu.lua very long? If not would you mind posting it?

my main.lua

local storyboard = require(“storyboard”);
storyboard.gotoScene(“menu”);
display.setStatusBar( display.HiddenStatusBar );
 

I think you misread my post. I was asking for your menu.lua  :) .

well here is my menu.lua

local storyboard = require(“storyboard”);
local widget = require(“widget”);
local scene = storyboard.newScene();
local screenGroup;
local buttonGroup = display.newGroup();

    local function StartButtonEventHandler(event)
        local phase = event.phase

        if event.phase == “ended” then
            print(“start button has been pressed”)
            storyboard.gotoScene(“scene1”, “slideLeft”, 100)
            return true
        end
    end

    local function OptionsButtonEventHandler(event)
        local phase = event.phase

        if event.phase == “ended” then
            print(“Options”)
            storyboard.gotoScene(“Options”, “slideDown”, 100)
            return true
        end
    end

function scene:createScene(event)
        screenGroup = self.view
        
        local background = display.newImage(“Images/Background.png”);
        background.x = display.contentCenterX;
        background.y = display.contentCenterY;

        screenGroup:insert(background)
    
    local StartButton = widget.newButton
    {
        left = 230,
        top = 235,
        defaultFile = “Buttons/Start.png”,
        overFile = “Buttons/StartOff.png”,
        onEvent = StartButtonEventHandler,
    }

    --local OptionsButton = widget.newButton
    --{
    --left = 250,
    --top = 300,
    --defaultFile = “Buttons/Options.png”,
    --overFile = “Buttons/OptionsOff.png”,
    --onEvent = OptionsButtonEventHandler,
    --}

    --buttonGroup:insert(StartButton)
    
    --buttonGroup:insert(OptionsButton)

end

function scene:enterScene(event)

end
    

function scene:exitScene(event)
    buttonGroup:removeSelf()
    buttonGroup = nil
    screenGroup:removeSelf()
    screenGroup = nil    
end

function scene:destroyScene(event)
end
        

scene:addEventListener(“createScene”);
scene:addEventListener(“enterScene”);
scene:addEventListener(“exitScene”);
scene:addEventListener(“destroyScene”);

return scene;

I’m starting to suspect that some of the images you have specified are not found.

Please check that the images are in the correct directories, and that the case is exactly as you specify in your code. 

Filenames are case-sensitive and must be named exactly as typed.

my images in Start button is correct, also in options button images. they’re exactly in the same folder. I’m really confuse, because as i said when i add buttons on my menu.lua file i get the same error. though when i add in my scene1.lua another button i don’t get an error. i tried recoding my menu.lua but still the same error. and this error is the only thing getting in the way of my project from progressing. T-T.

I copy/pasted your code and made a sample app. The only time I get the error you posted in your first post is when the widgets can’t find the images.

Just as a test,   delete all the overFile and defaultFile parameters from your code.

Example:

 local StartButton = widget.newButton { left = 230, top = 235, onEvent = StartButtonEventHandler, }

Remember. Do this for every button in your scene.

Does the app run then?

If it does, you must re-check all your image files very carefully. Like I mentioned above all files must have the same case as you have specified in your code. Example:  Buttons/OptionsOff.png is not the same as Buttons/optionsOff.png. Check your code *and* the actual file. The actual filename and the string in your code must be exactly the same.

Oh! thank you very much sir @ingemar.

turns out i was an idiot. i did not see the difference between the “OptionsOff.png” in my code to the file name “OptionOff.png”

Thank you very much!!!

No problem!

Glad that you found the reason :slight_smile:

Maybe add the event listener last