Jump to content




Please Help Me (lua) (error) attempt to index? ( a nil value)


  • You cannot reply to this topic
10 replies to this topic

#1 jay5476

  • Members
  • 289 posts

Posted 06 November 2012 - 08:28 PM

well ive tried my best to solve it now my program is in the hands of the community the error is on line 41

pastebin: www.pastebin.com/fnemsia2

#2 ChunLing

  • Members
  • 2,027 posts

Posted 06 November 2012 - 09:55 PM

I don't think that "text:len()" call works. At least, when I tried something similar I recall it not working.

#3 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 06 November 2012 - 09:58 PM

To get the length of text try using #text instead.

#4 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 07 November 2012 - 12:02 AM

View PostChunLing, on 06 November 2012 - 09:55 PM, said:

I don't think that "text:len()" call works. At least, when I tried something similar I recall it not working.
Posted Image

I didn't think it worked either, but I tried it out and it's valid here.

Maybe LuaJ is different.

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 07 November 2012 - 06:43 AM

View PostChunLing, on 06 November 2012 - 09:55 PM, said:

I don't think that "text:len()" call works. At least, when I tried something similar I recall it not working.

This works fine as long as text contains a valid string.

#6 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 07 November 2012 - 10:35 AM

It would be so much more helpful if CC had a full traceback for errors, so we could know where the function was called in the script when it errored.

While this doesn't exist, I believe I found the problem.

op = {" 1. Log In		", " 2. Register	  ", " 3. Change Pass   ", " 4. Shutdown	  ", " 5. Force Update  ", " 6. Credits	   "}

You have six options here,

for i = 1,7 do
term.setCursorPos(1, 4+i)
if i == sel then
cPrint("X--->".. op[i] .." <---X")
else
cPrint ( op[i] )
end
end

yet you try to move through it seven times. When you call cPrint() on it, lua tries to :len() on an option that doesn't exist. To prevent future problems, you should change 7 to #op

#7 jay5476

  • Members
  • 289 posts

Posted 20 November 2012 - 09:59 AM

k ty alot

#8 billysback

  • Members
  • 569 posts

Posted 20 November 2012 - 10:07 AM

The : works because strings are "objects" in lua, calling
string.function(string_value, x, y)
is made so that it can also be called as:
string_value:function(x, y)
as string_value is an instance of string, by calling string.function(string_value, x, y) you are basically calling:
string.function(self, x, y)
but applying self as a new instance of string,
string_value:function(x, y) is therefor equivelant to
string.function(self, x, y) as the ":" operator inserts the "self" parameter in to a function for "objects" in lua,

at least this is the explanation I believe.
I may be 100% wrong.

#9 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 20 November 2012 - 10:11 AM

You're actually perfectly right, billy. I didn't even think about that.

Though I'm confused as to why table:insert(element) doesn't work, but it works for strings. :/

#10 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 November 2012 - 11:31 AM

View Postbillysback, on 20 November 2012 - 10:07 AM, said:

The : works because strings are "objects" in lua, calling
string.function(string_value, x, y)
is made so that it can also be called as:
string_value:function(x, y)
as string_value is an instance of string, by calling string.function(string_value, x, y) you are basically calling:
string.function(self, x, y)
but applying self as a new instance of string,
string_value:function(x, y) is therefor equivelant to
string.function(self, x, y) as the ":" operator inserts the "self" parameter in to a function for "objects" in lua,

at least this is the explanation I believe.
I may be 100% wrong.

The string type probably has an implicit metatable __index to the string library. It may even be a table, under the hood. The colon operator works for any function contained in a table, and simply passes that table as the first argument to the function.

This doesn't work for tables because tables are not initialized with an __index metatable entry, which I would posit is a good thing.

#11 jay5476

  • Members
  • 289 posts

Posted 20 November 2012 - 11:46 AM

guys ive fixed it and still continuing devolping





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users