6 posts
Posted 01 January 2015 - 03:16 PM
I'm working on some basic UI creation in computercraft and wanted to follow an OO approach as that is my background. I followed some examples on the official lua documentation for writing classes in lua. I have this basic class, however, whenever I call the new method and pass in a table, I get the error: attempt to index a nil value. Here's the code snippet.
Button = {name = " " , text = " "}
function Button:new (o)
o=o or {}
setmetatable(o, self)
self.__index = self
return o
end
newButton = Button:new( {name = "test", text="test"})
I've shortened the code for simplicity, however, this still gives the error. I've even run this code in an online computercraft sim and it works. I'm wondering if this is possibly a limitation of the lua implementation used by computercraft?
Any help is much appreciated!
Thanks!
Edited on 02 January 2015 - 12:13 PM
3057 posts
Location
United States of America
Posted 02 January 2015 - 12:32 AM
What line do you get the error? The only thing I would change is changing the order of these lines:
self.__index = self
setmetatable( o, self )
Also, your parameter is probably an o, not a 0, and you have an open string on the last line. These are, I assume, simple typing errors. You wouldn't get attempt to index nil from either of these: I'd say the 0 oould cause it, if not for the o=o or {} line.
6 posts
Posted 02 January 2015 - 12:53 AM
Thanks for the response. Yes, those are typing errors. What i'm seeing is that the table I attempt to pass in as the parameter doesn't actually make it in. It's nil.
3057 posts
Location
United States of America
Posted 02 January 2015 - 01:43 AM
I realize that, I'm trying to figure out why: the o = o or {} line should guarantee that "o" is a table. Again, the line number would be helpful.
6 posts
Posted 02 January 2015 - 02:47 AM
I have edited the original post to fix the typos. The only line number I get the error on is the one that call the new function:
newButton = Button:new( {name "test", text="test"})
Thanks again for the help!
7083 posts
Location
Tasmania (AU)
Posted 02 January 2015 - 06:14 AM
Same thing if you add in the missing equals sign?
6 posts
Posted 02 January 2015 - 01:13 PM
Same thing if you add in the missing equals sign?
Yes, same issue. I'm so sorry for the typos.
The only other thing I should mention is that that the definition for Button and Button:new is in a separate file that i'm loading with os.loadAPI(). I wouldn't think that would matter.
6 posts
Posted 02 January 2015 - 01:32 PM
It seems that "Button" is what is showing as nil. How is that possible?
Ok, I have now tested this with all of the code in the same file and it seems that that was the problem. Are there known issues with os.loadAPI() not being able to handle certain things? This bums me out because I really didn't want to have to repeat my code all over the place.
Thanks for the help!!
Edited on 02 January 2015 - 12:33 PM
6 posts
Posted 02 January 2015 - 01:39 PM
Ok, folks. I'm so sorry for wasting your time. I just read further on the os.loadAPI and see that it puts the code inside a table named the name of the program. I just needed to prefix by calls to Button with that name.
Thanks again for the help!