attempt to index upvalue

I believe I have everything scoped properly but am told otherwise. The error message is “attempt to index upvalue ‘customlogoImg’ (a nil value)” and my code follows:

local customlogoImg

local function loadCustomLogo()
  customlogoImg = display.newImage("customlogo.png")
  customlogoImg.x = display.contentCenterX -- this is where the error appears
  customlogoImg.y = display.contentCenterY - 250
end

function scene:create( event )
  local appSceneGroup = self.view

  if (databox.customlogo == "1") then
    logoIsLoaded = 0
    network.download(databox.classurl .. "/img/customlogo.png", "GET", loadCustomLogo(), "customlogo.png")
  end
end

The error is called on the line indicated.

Any thoughts?

Double check the filename and path, including case, for your image and check the display object isn’t nil after creation before you do with on it.

Thank you for your response.

The display object is nil but I can’t figure out why. I verified that the file is on the remote server and is being fetched but after I call newImage(), the variable to which it’s assigned is nil.

Sorry I was on my phone, now I can see the whole code. You’re actually calling the ‘loadCustomLogo’ menthod in your network.download call as you’ve included the brackets.

Change it to this:

network.download(databox.classurl .. "/img/customlogo.png", "GET", loadCustomLogo, "customlogo.png")
  end

Also, you’ll want to check the event.phase in that loadCustomLogo function so change is declaration to this:

local function loadCustomLogo( event )

if ( event.isError ) then
        print( "Network error - download failed: ", event.response )
    elseif ( event.phase == "began" ) then
        print( "Progress Phase: began" )
    elseif ( event.phase == "ended" ) then
        print( "Displaying response image file" )
        customlogoImg = display.newImage("customlogo.png")
        customlogoImg.x = display.contentCenterX -- this is where the error appears
        customlogoImg.y = display.contentCenterY - 250
    end

end```

Additionally, network.download() will download the file to system.DocumentsDirectory unless another directory is specified, but display.newImage() defaults to looking in system.ResourceDirectory (unless another directory is specified).

Since you cannot download files to system.ResourceDirectory, you will need to change where newImage attempts to load an image from:
customlogoImg = display.newImage("customlogo.png", system.DocumentsDirectory)

Thank you and @Glitch_Games for your responses.

I’m closer but now I’m having trouble with getting network.download to even fetch the image within the emulator. The line of code is below, I’ve verified URLField.text and have the web server access and error logs tailed but it doesn’t bother making the request as instructed. The logo exists on the web server at the location specified but the request isn’t even being made.

      network.download("https://" .. URLField.text .. "/img/customlogo.png", "GET", networkListener, "customlogo.png", system.DocumentsDirectory)

What does the response from the network request say?

Try adding the printTable function from here to your project:

And then in your loadCustomLogo function (which is the network listener for your request), add the event arg to the function signature and print it out:

local function loadCustomLogo(event)
  print("loadCustomLogo")
  printTable( event )
  customlogoImg = display.newImage("customlogo.png")
  customlogoImg.x = display.contentCenterX -- this is where the error appears
  customlogoImg.y = display.contentCenterY - 250
end

If there’s an error making the request, it should be somewhere in the event table. If not an obvious error message, then an HTTP code which will explain the reason for the failure.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.