Attempt to index local 'photo' (a nil value)

Keep having this error message :

Line: 2050

Attempt to index local ‘photo’ (a nil value) 

Here is my code (part of it) : 

 function btnmyBtn(self) --External code local onComplete = function(event) -- Create local variable named photo local photo = event.target photo:setReferencePoint(display.TopLeftReferencePoint); -- Place photo over the body photo.x = 0; photo.y = 0; -- Scale down the photo to match the body scale -- Note that 0.07 scale is ideal for iPad 3rd Generation camera -- Other cameras may need different scaling photo.xScale = display.contentScaleX photo.yScale = display.contentScaleY -- Remove the lyrhead image placeholder from the group named "guy" guy:remove(lyrhead); -- Insert the newly taken picture in place of the lyrhead guy:insert(photo); end -- If a camera exists on the device, start camera function if media.hasSource( media.Camera ) then media.show( media.Camera, onComplete ) else -- If camera doens't exist, show alert popup native.showAlert( "Corona", "This device does not have a camera.", { "OK" } ) end end 

And here is the line 2050 : 

photo:setReferencePoint(display.TopLeftReferencePoint);

Any advice will help.

Thank you

It appears you are declaring the photo object locally within another function, which will keep it from being used as an object for an event listener. Using another object that is already declared outside of the function will remove the error, or you could forward declare photo and not make it local within the function itself. This might bring other errors, but it would likely resolve the one you are currently receiving.

Hi,

Thanks for your reply.

Do you have example to show me?

Thank you

To begin, I’d suggest reading through the Corona documentation on event listeners and how they work with objects:

http://docs.coronalabs.com/guide/events/detectEvents/index.html

In the short term, you could do this:

local photo function btnmyBtn(self) --External code local onComplete = function(event) -- Create local variable named photo photo = event.target

I haven’t tested the above, don’t know it it’ll work, your mileage may vary blah blah blah :wink:

Seriously, if you set a listener for an object that only exists within the function which is being called, it’s never going to work. I’d fiddle around with the above and make sure that it reflects the Corona SDK best practices when it comes to setting event listeners. Good luck!

I tried what you showed me and you were right.

I have now this error 

Line2050

Attempt to index upvalue ‘photo’ (a nil value)

Anyway thanks ; )

Yea, you’re going to be running into errors like that quite a bit if you are trying to structure your code in this way. Really the very best thing for you to do is take a look at the event listener documentation to make sure you’re structuring code correctly. 

I ran into the same issues at first. A good exercise would be to deconstruct the code you have written. Start with the btnmyBtn function by itself. Make sure it works with no errors. Then you can work on the onComplete code. Once that is working with no errors, throw both those bad boys together and see what happens.

There is really no substitute for experience. Refer to the documentation often and you’ll be flying in no time!

OK i will try.

This is gonna be a long day!!

Thanks

local onComplete = function(event)     -- Create local variable named photo     local photo = event.target     if photo == nil then          -- do something to handle the error condition.     else         photo:setReferencePoint(display.TopLeftReferencePoint);         -- Place photo over the body         -- rest of your function code    end end

What if they cancel the camera and don’t do anything?  You have to catch that error condition.

This is actually where i get my error on the simulator.

When i cancel the error appear!

Right.  When you cancel. you have no photo.  Therefore event.target is nil.  You copy that nil to the photo variable then you try and access it by setting it’s reference point.  It’s not a display object, therefore your app crashes.  Put that if statement in and if you don’t need to do anything you can leave the part between the if and the else blank or you could put a

     return false

in there just to abort the rest of the function and return to your app.

but if i leave the part between the if and the else blank, nothing is gonna change and i will have the error again?

Should i write it this way : 

local onComplete = function(event) -- Create local variable named photo local photo = event.target if photo == nil then return false -- do something to handle the error condition. else photo:setReferencePoint(display.TopLeftReferencePoint); -- Place photo over the body -- rest of your function code end end

Thank for your help

It appears you are declaring the photo object locally within another function, which will keep it from being used as an object for an event listener. Using another object that is already declared outside of the function will remove the error, or you could forward declare photo and not make it local within the function itself. This might bring other errors, but it would likely resolve the one you are currently receiving.

Hi,

Thanks for your reply.

Do you have example to show me?

Thank you

To begin, I’d suggest reading through the Corona documentation on event listeners and how they work with objects:

http://docs.coronalabs.com/guide/events/detectEvents/index.html

In the short term, you could do this:

local photo function btnmyBtn(self) --External code local onComplete = function(event) -- Create local variable named photo photo = event.target

I haven’t tested the above, don’t know it it’ll work, your mileage may vary blah blah blah :wink:

Seriously, if you set a listener for an object that only exists within the function which is being called, it’s never going to work. I’d fiddle around with the above and make sure that it reflects the Corona SDK best practices when it comes to setting event listeners. Good luck!

I tried what you showed me and you were right.

I have now this error 

Line2050

Attempt to index upvalue ‘photo’ (a nil value)

Anyway thanks ; )

Yea, you’re going to be running into errors like that quite a bit if you are trying to structure your code in this way. Really the very best thing for you to do is take a look at the event listener documentation to make sure you’re structuring code correctly. 

I ran into the same issues at first. A good exercise would be to deconstruct the code you have written. Start with the btnmyBtn function by itself. Make sure it works with no errors. Then you can work on the onComplete code. Once that is working with no errors, throw both those bad boys together and see what happens.

There is really no substitute for experience. Refer to the documentation often and you’ll be flying in no time!

OK i will try.

This is gonna be a long day!!

Thanks

local onComplete = function(event)     -- Create local variable named photo     local photo = event.target     if photo == nil then          -- do something to handle the error condition.     else         photo:setReferencePoint(display.TopLeftReferencePoint);         -- Place photo over the body         -- rest of your function code    end end

What if they cancel the camera and don’t do anything?  You have to catch that error condition.

This is actually where i get my error on the simulator.

When i cancel the error appear!

Right.  When you cancel. you have no photo.  Therefore event.target is nil.  You copy that nil to the photo variable then you try and access it by setting it’s reference point.  It’s not a display object, therefore your app crashes.  Put that if statement in and if you don’t need to do anything you can leave the part between the if and the else blank or you could put a

     return false

in there just to abort the rest of the function and return to your app.