简体   繁体   中英

Attempt to call global function is nil, but function is not shown in debugger?

I am using Eclipse LDT for development, using the packaged Lua EE and Interpreter for Lua 5.2. I need to call the commandDatabase() method from my main method, though when I try, I receive the error:

"attempt to call global 'commandDatabase' (a nil value)".

I have looked up this error, and I am, as far as I can tell, defining methods in the right order. Lua - attempt to call global 'contains' (a nil value)

When I view it in the debugger, the interpreter does not seem to find any methods I define between commandSystem and commandHelp . It shows each other function in the Variables area as eg ["commandSystem"] = function() but commandDatabase() does not appear

I have tried calling a wrapper method like so:

function commandDatabaseStep()
  return commandDatabase()
end

... but this did not work either (same error)

The relevant code:


-- System command
function commandSystem()
  ...
end

-- Database command
function commandDatabase()

  if arguments[2] == "no_arg1" then
    print("The 'database' command must have at least one argument: [generate, wipe, dump, delete, get <system_name>]", true)
    return 2
    
  elseif arguments[2] == "generate" then
    local file = io.open(database, "w+")
    file:write("#ssmhub database")
    file:close(file)
    return 1

  elseif arguments[2] == "dump" then
    print("= DUMP START =")
    for line in io.lines(database) do
      print(line)
    end
    print("= DUMP  END  =")
    return 1

  -- 1+
  elseif arguments[2] == "get" then
  
    -- 2+
    if isEmpty(arguments[3]) then
      print("The 'database get' command must have a <name> parameter")
      return 0 
      -- 2-
    else -- 3+
      local file = io.open(database, "r")
      
      for line in io.lines(file) do -- 4+
        local state = ""
        local id = ""
        local dividersFound = 0

        line:gsub(".", function(c) -- 5+
                 
          if c == "|" then -- 6+
            
            if dividersFound == 0 then -- 7+
              state = state .. c
            end -- 7-
            if dividersFound == 1 then -- 8+
              id = id .. c
            end -- 8-
            dividersFound = dividersFound + 1
            
          end -- 6-
          
        end) -- 5-

        io.close(file)

      end -- 4-
    end -- 3-
    else  -- 9+
      print("Illegal argument for command. Use 'help' for a list of commands and arguments.")
      return 0
    end -- 9-
  end -- 2-
end -- 1-

function commandHelp()
  ...
end

-- Main
function main()
  arguments = readProgramArguments()

  commandArgument = arguments[1]

  commandCompleteCode = 0

  -- Process help and system commands

  if commandArgument == "database" then
    commandCompleteCode = commandDatabase()
  end
end main()

As @luther pointed out, I had one-too-many end-s in commandDatabase .

This wasn't flagged in my IDE because I had not end-ed commandSystem , so commandSystem was nested inside of it.

To fix: add an end to commandSystem , and remove the end which I tagged '-- 1-'.

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