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

attempt to index ? (a nil value)

Started by NeonPhoenix, 10 June 2012 - 10:26 AM
NeonPhoenix #1
Posted 10 June 2012 - 12:26 PM
I am trying to create a auto-crafting system and i am currently having a small problem with this code


Spoiler
version = "v0.1"

function homescreen()
term.clear()
term.setCursorPos(1,1)
print(string.rep("*", 47))
term.setCursorPos(1,10)
end

function getInput(intype, character)
write(intype)
amount = read(character)
return amount
end

function home()
homescreen()
print("How many do you want to make?")
amount = getInput("Amount: ")
if amount <= "4" then
rs.setBundledOutput("left", color.white)
rs.setBundledOutput("left", color.black)
sleep(1)
rs.setBundledOutput("left", 0)
shell.run(Auto-Workbench)
elseif amount >= "5" and amount <= "8" then
rs.setBundledOutput("left", color.white)
rs.setBundledOutput("left", color.black)
sleep(2)
rs.setBundledOutput("left", 0)
shell.run(Auto-Workbench)
elseif amount >= "9" and amount <= "12" then
rs.setBundledOutput("left", color.white)
rs.setBundledOutput("left", color.black)
sleep(1)
rs.setBundledOutput("left", 0)
shell.run(Auto-Workbench)
elseif amount >= "13" and amount <= "16" then
rs.setBundledOutput("left", color.white)
rs.setBundledOutput("left", color.black)
sleep(1)
rs.setBundledOutput("left", 0)
shell.run(Auto-Workbench)
end
end

home()



every time i run this script i keep getting
torch:21: attempt to index ? (a nil value)

please help
Cloudy #2
Posted 10 June 2012 - 12:52 PM
Change all references to the "color" API to "colors".
NeonPhoenix #3
Posted 10 June 2012 - 01:52 PM
Thanks that seem to have worked
Lyqyd #4
Posted 10 June 2012 - 06:04 PM
Also, you really should return tonumber(amount) in function getInput and then compare the number to actual numbers, not string literals.
blipman17 #5
Posted 11 June 2012 - 08:09 PM
i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler

term.clear()
print("\n\n\n##################################################\n")
print("computercraft version;   ", os.version())
print("Computer ID;			 ", os.computerID())
print("This computer is called; ", os.getComputerLabel())
lounchstartuppr=false
    if fs.exists("startupprogram.txt")~="false" then
	    fs.open("path/to/file", "r")
	    local stexecute = file.readAll()
	    file.close()
	    lounchstartuppr=true
    else
	    print("opening on startup;	  -")
    end
print("##################################################\n")
sleep(3)
    if lounchstartuppr==true then
	    print("starting program")
	    sleep(1)
	    shell.run(stexecute)
    end
term.clear()
term.setCursorPos(1, 1)
MysticT #6
Posted 11 June 2012 - 08:25 PM
i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler

term.clear()
print("nnn##################################################n")
print("computercraft version;   ", os.version())
print("Computer ID;			 ", os.computerID())
print("This computer is called; ", os.getComputerLabel())
lounchstartuppr=false
	if fs.exists("startupprogram.txt")~="false" then
		fs.open("path/to/file", "r")
		local stexecute = file.readAll()
		file.close()
		lounchstartuppr=true
	else
		print("opening on startup;	  -")
	end
print("##################################################n")
sleep(3)
	if lounchstartuppr==true then
		print("starting program")
		sleep(1)
		shell.run(stexecute)
	end
term.clear()
term.setCursorPos(1, 1)
You should make your own post about this.
The problem is here:

if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:

if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:

local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:

fs.open("/path/to/file", "r")
to:

local file = fs.open("startupprogram.txt", "r")
blipman17 #7
Posted 11 June 2012 - 08:34 PM
i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler

term.clear()
print("nnn##################################################n")
print("computercraft version;   ", os.version())
print("Computer ID;			 ", os.computerID())
print("This computer is called; ", os.getComputerLabel())
lounchstartuppr=false
	if fs.exists("startupprogram.txt")~="false" then
		fs.open("path/to/file", "r")
		local stexecute = file.readAll()
		file.close()
		lounchstartuppr=true
	else
		print("opening on startup;	  -")
	end
print("##################################################n")
sleep(3)
	if lounchstartuppr==true then
		print("starting program")
		sleep(1)
		shell.run(stexecute)
	end
term.clear()
term.setCursorPos(1, 1)
You should make your own post about this.
The problem is here:

if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:

if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:

local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:

fs.open("/path/to/file", "r")
to:

local file = fs.open("startupprogram.txt", "r")
i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler

term.clear()
print("nnn##################################################n")
print("computercraft version;   ", os.version())
print("Computer ID;			 ", os.computerID())
print("This computer is called; ", os.getComputerLabel())
lounchstartuppr=false
	if fs.exists("startupprogram.txt")~="false" then
		fs.open("path/to/file", "r")
		local stexecute = file.readAll()
		file.close()
		lounchstartuppr=true
	else
		print("opening on startup;	  -")
	end
print("##################################################n")
sleep(3)
	if lounchstartuppr==true then
		print("starting program")
		sleep(1)
		shell.run(stexecute)
	end
term.clear()
term.setCursorPos(1, 1)
You should make your own post about this.
The problem is here:

if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:

if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:

local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:

fs.open("/path/to/file", "r")
to:

local file = fs.open("startupprogram.txt", "r")
i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler

term.clear()
print("nnn##################################################n")
print("computercraft version;   ", os.version())
print("Computer ID;			 ", os.computerID())
print("This computer is called; ", os.getComputerLabel())
lounchstartuppr=false
	if fs.exists("startupprogram.txt")~="false" then
		fs.open("path/to/file", "r")
		local stexecute = file.readAll()
		file.close()
		lounchstartuppr=true
	else
		print("opening on startup;	  -")
	end
print("##################################################n")
sleep(3)
	if lounchstartuppr==true then
		print("starting program")
		sleep(1)
		shell.run(stexecute)
	end
term.clear()
term.setCursorPos(1, 1)
You should make your own post about this.
The problem is here:

if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:

if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:

local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:

fs.open("/path/to/file", "r")
to:

local file = fs.open("startupprogram.txt", "r")

i am going to make a new post.

about that "path/to/file"
i tried some thins on the internet, but could'nt figure it out an i think that i left it accidentally.

it is not yet solved by your change, but i know the file exists and has a value
blipman17 #8
Posted 11 June 2012 - 08:37 PM
i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler

term.clear()
print("nnn##################################################n")
print("computercraft version;   ", os.version())
print("Computer ID;			 ", os.computerID())
print("This computer is called; ", os.getComputerLabel())
lounchstartuppr=false
	if fs.exists("startupprogram.txt")~="false" then
		fs.open("path/to/file", "r")
		local stexecute = file.readAll()
		file.close()
		lounchstartuppr=true
	else
		print("opening on startup;	  -")
	end
print("##################################################n")
sleep(3)
	if lounchstartuppr==true then
		print("starting program")
		sleep(1)
		shell.run(stexecute)
	end
term.clear()
term.setCursorPos(1, 1)
You should make your own post about this.
The problem is here:

if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:

if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:

local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:

fs.open("/path/to/file", "r")
to:

local file = fs.open("startupprogram.txt", "r")

i changed the code but it is still not working,
i know the file exists and has "chestdispencer" written in it.

about the "path/to/file" thing, i left that accidentally when i tried things on the internet

but it is a good idea to post this as an other tread