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

Attempt to get length of nil [lua] [error]

Started by funkey100, 05 April 2012 - 12:52 AM
funkey100 #1
Posted 05 April 2012 - 02:52 AM
In my Dual Boot OS, I keep getting the error of "attempt to get length of nil". I get that error in line 38 (for i=1, #m, 1 do), which is part of a function which I have two of. It's the second of the same function which basicly is a menu with different options. Can anyone help me?


d=""
n=1
nn=1
if fs.exists("/DualBootOS") then d=""
elseif fs.exists("disk/DualBootOS") then d = "disk"
elseif fs.exists("disk2/DualBootOS") then d = "disk2"
elseif fs.exists("disk3/DualBootOS") then d = "disk3"
elseif fs.exists("disk4/DualBootOS") then d = "disk4"
elseif fs.exists("disk5/DualBootOS") then d = "disk5"
elseif fs.exists("disk6/DualBootOS") then d = "disk6"
end
function OSMenu(m)
while true do
term.clear()
term.setCursorPos(1,1)
print("Dual Boot OS")
term.setCursorPos(1,3)
for i=1, #m, 1 do
if i==n then print(i, " -->"..m[i]) else print(i, "	", m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<4 then n=n+1 end
if b==28 then break end
end
term.clear()
term.setCursorPos(1,1)
end
end
function installMenu(m)
while true do
term.clear()
term.setCursorPos(1,1)
print("Select operating system to install on computer")
term.setCursorPos(1,3)
for i=1, #m, 1 do
if i==nn then print(i, " -->"..m[i]) else print(i, "	", m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then nn=nn-1 end
if b==208 and n<4 then nn=nn+1 end
if b==28 then break end
end
term.clear()
term.setCursorPos(1,1)
end
end
function choice(f)
term.clear()
term.setCursorPos(1,1)
if f==1 then
if n==1 then print("CraftOS 1.3")
shell.run(d.."/DualBootOS/CraftOS/startup")
elseif n==2 then shell.run(d.."/DualBootOS/BlahOS/startup")
elseif n==3 then
installMenu(ioptions)
choice(2)
elseif n==4 then os.shutdown()
end
elseif f==2 then
term.clear()
term.setCursorPos(1,1)
if nn==1 then
if fs.exists("startup") then
writeLine("Deleting startup")
fs.delete("startup")
endwrite("[DONE]")
writeLine("Done installing, restarting computer")
print("3...")
sleep(1)
print("2...")
sleep(1)
print("1...")
sleep(1)
os.reboot()
else
print("You already have CraftOS installed on your computer!")
print("Try removing any disks with the file startup on the root.")
print("Shutting down")
sleep(3)
os.shutdown()
end
elseif nn==2 then shell.run(d.."/DualBootOS/BlahOS/install") elseif nn==3 then
elseif nn==3 then
elseif nn==4 then
end
end
end
local options={
"CraftOS 1.3",
"Blah OS 0.4",
"Install OS on computer",
"Exit"}
local ioptions={
"CraftOS 1.3",
"Blah OS 0.4",
"Dual Boot OS",
"Back"}
OSMenu(options)
choice(1)
Cloudy #2
Posted 05 April 2012 - 03:21 AM
"m" isn't defined before you start your program. "m" should be a table.
funkey100 #3
Posted 05 April 2012 - 03:26 AM
function installMenu(m) --This declares my function
installMenu(ioptions) --This is how I started my function
M should be replaced by ioptions.
funkey100 #4
Posted 05 April 2012 - 03:33 AM
I just noticed I don't need the installMenu(m) function because the otherone is exactly the same except for it uses n instead of nn and I say which table I want to use. I'm stupid…
Luanub #5
Posted 05 April 2012 - 03:36 AM
From what I see in the logic of the script your launching OSMenu() first which is using the options var as its argument which is a string, the for loop is looking for a number.
funkey100 #6
Posted 05 April 2012 - 03:42 AM
Thats why I used #, I think it returns the number of strings inside the table. I tried editting the code a bit, same error but on line 18.

d=""
if fs.exists("/DualBootOS") then d=""
elseif fs.exists("disk/DualBootOS") then d = "disk"
elseif fs.exists("disk2/DualBootOS") then d = "disk2"
elseif fs.exists("disk3/DualBootOS") then d = "disk3"
elseif fs.exists("disk4/DualBootOS") then d = "disk4"
elseif fs.exists("disk5/DualBootOS") then d = "disk5"
elseif fs.exists("disk6/DualBootOS") then d = "disk6"
end
function OSMenu(m)
n=1
while true do
term.clear()
term.setCursorPos(1,1)
print("Dual Boot OS")
term.setCursorPos(1,3)
for i=1, #m, 1 do
if i==n then print(i, " --> "..m[i]) else print(i, "      ", m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<4 then n=n+1 end
if b==28 then break end
end
term.clear()
term.setCursorPos(1,1)
end
end
function choice(f)
term.clear()
term.setCursorPos(1,1)
if f==1 then
if n==1 then print("CraftOS 1.3")
shell.run(d.."/DualBootOS/CraftOS/startup")
elseif n==2 then shell.run(d.."/DualBootOS/BlahOS/startup")
elseif n==3 then
OSMenu(ioptions)
choice(2)
elseif n==4 then os.shutdown()
end
elseif f==2 then
term.clear()
term.setCursorPos(1,1)
if nn==1 then
if fs.exists("startup") then
writeLine("Deleting startup")
fs.delete("startup")
endwrite("[DONE]")
writeLine("Done installing, restarting computer")
print("3...")
sleep(1)
print("2...")
sleep(1)
print("1...")
sleep(1)
os.reboot()
else
print("You already have CraftOS installed on your computer!")
print("Try removing any disks with the file startup on the root.")
print("Shutting down")
sleep(3)
os.shutdown()
end
elseif nn==2 then shell.run(d.."/DualBootOS/BlahOS/install") elseif nn==3 then
elseif nn==3 then
elseif nn==4 then
OSMenu(options)
choice(1)
end
end
end
local options={
"CraftOS 1.3",
"Blah OS 0.4",
"Install OS on computer",
"Exit"}
local ioptions={
"CraftOS 1.3",
"Blah OS 0.4",
"Dual Boot OS",
"Back"}
OSMenu(options)
choice(1)
Cloudy #7
Posted 05 April 2012 - 03:45 AM
"m" still isn't defined, so how can #m get the length of the non existant table m?
funkey100 #8
Posted 05 April 2012 - 03:46 AM
:facepalm:
m stands for options or ioptions!
For example, f for the choice function isn't defined but it works when clicking Blah OS or CraftOS.
Luanub #9
Posted 05 April 2012 - 03:54 AM
You might have to do a return on the table prior to using it like that.. Try adding a return #m prior to your loop.
funkey100 #10
Posted 05 April 2012 - 04:01 AM
So like this?

return #m
while true do
Luanub #11
Posted 05 April 2012 - 04:18 AM
As long as its in the function and prior to the for loop it should work. I'm not 100% sure that's the problem, I'm not to where I can test anything at the moment.
funkey100 #12
Posted 05 April 2012 - 04:19 AM
I'll test it tomorrow, going to sleep soon and on iPod.
Luanub #13
Posted 05 April 2012 - 10:21 AM
Yeah don't bother with the return its not what you needed.

I finally got home and could play with it. You simply need to do as Cloudy stated and define the var m as a table in the start of your code

change

d=""
n=1
nn=1
if fs.exists("/DualBootOS") then d=""


to

d=""
n=1
nn=1
m = {} -- defines m as a table which will then allow you to # it
if fs.exists("/DualBootOS") then d=""


I got the menu to come up by doing this. I did not test it beyond that.
funkey100 #14
Posted 05 April 2012 - 04:09 PM
But I'm using the function twice for different tables. If I leave it how it is, the first menu will show up but if I select option 3, which opens the second menu, it gives the error.
MysticT #15
Posted 05 April 2012 - 07:15 PM
The problem is in these lines:

OSMenu(ioptions)
...
OSMenu(options)
you call that function with variables that are not defined. I know you did define them, but you did it at the end, so inside the functions, those variables doesn't exist.
Try moving the definition of the tables to the top, it should fix that problem.
funkey100 #16
Posted 05 April 2012 - 07:43 PM
Oh! Thanks! The first menu worked because I declared it before I ran it but the second menu was declared afterward! I'll try that when I get home.
funkey100 #17
Posted 06 April 2012 - 02:47 AM
I'll be releasing the first version of Dual Boot OS soon… :)/>/>