This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Wolverines84's profile picture

Error with classes :new function, attempt to index a nil value

Started by Wolverines84, 01 January 2015 - 02:16 PM
Wolverines84 #1
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
KingofGamesYami #2
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.
Wolverines84 #3
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.
KingofGamesYami #4
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.
Wolverines84 #5
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!
Bomb Bloke #6
Posted 02 January 2015 - 06:14 AM
Same thing if you add in the missing equals sign?
Wolverines84 #7
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.
Wolverines84 #8
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
Wolverines84 #9
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!