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

Attempt To Index ? (A Nil Value)

Started by rockymc, 14 February 2012 - 07:48 PM
rockymc #1
Posted 14 February 2012 - 08:48 PM
I'm getting the error in the title with this code:



function get(program)
shell.run('cc-get', 'install', program)
end

function c()
term.clear()
term.setCursorPos(1, 1)
end

function sp(text)
textutils.slowPrint(text)
end

function getVersion()
return "0.1"
end

function p(text)
print(text)
end

c()

sp("RocInstall " ..getVersion())
sleep(1)
c()

p("(1) Install\n(2) About\n(3) Instructions\n(4) Exit")
e, a = os.pullEvent()
if e == "key" and a == 2 then
c()
sp("Program Name: ")
name = read()
get(name)
fs.copy("bin/"..name, "RI")
sp(name.. " was installed to directory RI.")
end

Any fix?
Advert #2
Posted 14 February 2012 - 10:17 PM
I'm getting the error in the title with this code:



function get(program)
shell.run('cc-get', 'install', program)
end

function c()
term.clear()
term.setCursorPos(1, 1)
end

function sp(text)
textutils.slowPrint(text)
end

function getVersion()
return "0.1"
end

function p(text)
print(text)
end

c()

sp("RocInstall " ..getVersion())
sleep(1)
c()

p("(1) Installn(2) Aboutn(3) Instructionsn(4) Exit")
e, a = os.pullEvent()
if e == "key" and a == 2 then
c()
sp("Program Name: ")
name = read()
get(name)
fs.copy("bin/"..name, "RI")
sp(name.. " was installed to directory RI.")
end

Any fix?

Some suggestions:

sp = textutils.slowPrint
Use os.pullEvent() in a loop.

I'm not sure where you're getting the Attempting to index nil error; try fixing these issues, and see if it fixes the problem for you.

If it doesn't, follow Espen's instructions in this post, and you should get the location of your error.
rockymc #3
Posted 15 February 2012 - 04:09 PM
Uhh, when I write the name of the program to install it gives me that error.
Advert #4
Posted 15 February 2012 - 04:42 PM
Uhh, when I write the name of the program to install it gives me that error.
I can't see where the code is erroring.

Try debugging it: comment out various sections of code, to see where it errors. Alternatively, you can, as stated in my previous post, follow Espen's instructions in this post, or download Casper's debugger, in order to locate the line where the error is happening.
I would first comment out:

shell.run('cc-get', 'install', program)

Also, I would really recommend not shortening "print" to "p", and so fourth. It makes your code less readable.
If you would still like to do that, I would recommend doing it this way:

p = print
as you don't get the overhead of an extra function call.

I would also recommend that you use os.pullEvent in a loop; if the terminal receives redstone power while a user is attempting to install, your program may just exit.