Jump to content




[Error] attempt to get length of nil


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

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 16 August 2012 - 01:08 AM

I have been using this function for a while, in several different programs, but now is the only time it seems to give me an error. It is supposed to print a menu, center screen, for however long the reference table is.
function opt(m)
local n=1
local l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
local b = string.len(m[i])/2
local x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(m[i]) end
end
local a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
I have tried to have it reference any table I can think of, but it doesn't work. Like I said, it used to work but does not anymore. Any ideas?

#2 Kazimir

  • Members
  • 90 posts
  • Locationthat's no moon...

Posted 16 August 2012 - 06:20 AM

try so:
function opt(tabl)
tabl=m


#3 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 16 August 2012 - 06:55 AM

use
m=m or {}


#4 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 16 August 2012 - 12:41 PM

So i found out I was calling back to the function incorrectly. It was in a nested part of my program, and couldn't see it properly. By the way, modified the code slightly to keep it from running off of the page:
function opt(m)
local n=1
local l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
local b = string.len(m[i])/2
local x = (x/2)-b
term.setCursorPos(x,i+7)
term.clearLine()
print(m[i]) end
end
local a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end --slight modification from <=
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end


#5 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 16 August 2012 - 12:49 PM

why don't you do the same thing but add
m=m or {}
at the beginning so if it doesn't get a first parameter it sets it to be an empty table rather than crashing?

#6 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 16 August 2012 - 12:55 PM

Oh, yeah, I will take that into consideration.

#7 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 16 August 2012 - 01:02 PM

or you can have an if statement to check its type and return nil if it isn't a table
if type(m)~="table" then
print("incorrect usage")
if m then print(m) end
return nil
end






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users