user916601
user916601

Reputation:

Lua: attempt to index field '?' (a nil value)

I am just starting with lua, and i have some code, from someone with some intentionally mistakes in it. Now i've hit a roadblock getting this error on and on for the following code:

function SIM_Utils:ClickButton(app, buttonName)
        page = app:getTopPage()
        widgets = page:getWidgets(buttonName)
        print (type(widgets))
        print (widgets[1])
        widgets[1]:click(true, 5000)--this yields "attempt to index field '?' (a nil value)"
        widgets[1]:click(false,0)--this yields "attempt to index field '?' (a nil value)"
        app:captureScreen()
end

This will result in:

table
WidgetCommon (09590790)

L.E.

After running what Alex posted here is the result:

widgets=

table: 0A45CF28

1

WidgetCommon (09590790)

Is table: 0A... the answer i am looking for?

L.E. 2: Reposted the whole function since it seems this is where the problem lays

Upvotes: 0

Views: 9608

Answers (3)

Mark Langen
Mark Langen

Reputation: 389

It seems unlikely, but it's possible (and it in fact looks like the only possibility from the info you've posted) that the library you're using is throwing the error, not your code. Be aware that when an error is thrown in Lua, the throwing code and tell Lua to show the error as thrown from your calling code, rather than in the Library code. That should only be used for logical errors like "Bad arguments to Widget::show", not something like what you're getting, but it can happen. Also, an error in a C function will show up sort of like it came from your code. EG:

io.open("myFile").read() --> should be ":read", not ".read"
stdin:1: attempt to index a nil value
stack traceback:
    stdin:1: in main chunk
    [C]: ?

Do you have a stack traceback to show as well as the error?

Upvotes: 0

Alex
Alex

Reputation: 15313

Try running the following code:

widgets = page:getWidgets(buttonName) 
print("widgets=", widgets) -- Should print out something like "Widgets= 0x12345678". If it doesn't then widgets is nil. In other words nothing is there. 
for k, v in pairs(widgets) do
    print(k, v)
end

That will make sure you make sure you have a widgets table, and if you do, it'll tell you whats in it. If you don't have anything at an index of 1, then that's your problem: page:getWidgets(buttonName) is not returning a list of widgets like you expect.

Upvotes: 0

BMitch
BMitch

Reputation: 263469

What is page:getWidgets returning? You can check it with print(type(widgets)). If it is a table, then array position 1 is not defined in that table (you can loop through the table contents using the pairs function). If it's not a table, then you're attempting to lookup an index on something that isn't a table, which won't work.

Also, since you're new to Lua, realize that page:getWidgets is not a built-in component. So, you'll need to load this functionality or use the appropriate derived application that provides this function.

Upvotes: 1

Related Questions