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

[Resolved][Error] attempt to index ?

Started by Matt21, 31 January 2013 - 05:26 AM
Matt21 #1
Posted 31 January 2013 - 06:26 AM
Error:

42:attempt to index ? (a nil value)

There is the entire code:


function clear()
term.clear()
term.setCursorPos(1,1)
end
function menuEnding()
if menuStat == 2 then menuStat = 0
elseif menuStat == -1 then menuStat = 1 end
end
function makeDirs()
fs.makeDir("MTBOs")
fs.makeDir("MTBOs/installdata")
fs.makeDir("MTBOs/core")
end
function fError(typ, msg)
clear()

if typ == "d" then
  while true do
   clear()
   print("Erreur:")
   print(msg)
   print("Merci de contacter un admin")
   sleep(99)
  end
end

end
function webInstall()

function downloadfile(url, file, name)
  print(url)
  response = http.get(url)
  if response then print (name.." get.")
  else fError("d","Impossible de télécharger le fichier: "..name) end
  dFile = response.readAll()
  response.close()
  sfile = fs.open(file, "w")
  sfile.write(dFile)
  sfile.close()
end

function readList()
  file = fs.open("MTBOs/installdata/list.data", "r")
  files2down = { }
  repeat
   line = file.readLine()
   files2down[#files2down + 1] = line
  until line == nil
end

function downloadFiles()
  print(files2down[1])
  downloadfile(files2down[1], "MTBOs/core/", "firstmenu")
  -------------------------------------------------------------------- LISTE A METTRE A JOUR
end

function WIMain()
  clear()
  makeDirs()
  downloadfile("http://pastebin.com/raw.php?i=daaXMkX4", "MTBOs/installdata/list.data", "list") -- Télécharger la liste
  readList()
  print(files2down[1])
  downloadFiles()
end

WIMain()

end
function diskInstall()

function copy(from, to)
  fs.copy("disk/mtbinstall/"..from, "MTBOs/"..to)
end

function copyFiles()
  copy("core/firstmenu", "core/firstmenu")
  ------------------------------------------------------------- LISTE A METTRE A JOUR
end

clear()
makeDirs()
copyFiles()

end
function startup()

vars()

menuStat = 0
repeat
  clear()
  term.setCursorPos(1, 1)
  print ("MTB Os "..ver)
  pressedOk = false
  if menuStat == 0 then
   print[[> Installation a partir d'un disque]]
   print[[	Inserez un disk d'installation dans un disk]]
   print[[	drive à coté de l'ordinateur]]
   print[[	Vous pouvez vous procurer un disque]]
   print[[	d'installation auprès d'un admin/modo]]
   print[[  Installation par internet]]
   print[[	Se connecter à internet pour télécharger tous]]
   print[[	les fichiers nécéssaires]]
   elseif menuStat == 1 then
   print[[  Installation a partir d'un disque]]
   print[[	Inserez un disk d'installation dans un disk]]
   print[[	drive à coté de l'ordinateur]]
   print[[	Vous pouvez vous procurer un disque]]
   print[[	d'installation auprès d'un admin/modo]]
   print[[> Installation par internet]]
   print[[	Se connecter à internet pour télécharger tous]]
   print[[	les fichiers nécéssaires]]
  end
  event, param1 = os.pullEvent("key")
  if param1 == 208 then menuStat = menuStat + 1
   elseif param1 == 200 then menuStat = menuStat - 1
  elseif param1 == 28 then pressedOk = true end
  menuEnding()
until pressedOk == true

if menuStat == 1 then webInstall() end

end
function vars()
ver = "1.0"
end
startup()

I don't understand why it don't work :(/>

Thanks
KaoS #2
Posted 31 January 2013 - 06:38 AM
in the function readList() you define the files2down table and in the downloadFiles() and WIMain() functions you access it. if you run downloadFiles() or WIMain() before you have run downloadFiles() then files2down is still nil and will error. I would add

local files2down={}
at the top of the program
remiX #3
Posted 31 January 2013 - 06:39 AM
Hmm, what exactly is line 42 for you before pasting it here?

Maybe try http.request instead of http.get?

EDIT:

in the function readList() you define the files2down table and in the downloadFiles() and WIMain() functions you access it. if you run downloadFiles() or WIMain() before you have run downloadFiles() then files2down is still nil and will error. I would add

local files2down={}
at the top of the program

But he calls readFiles before readList() before other functions that need the table.

Also:
files2down[#files2down + 1] = line
would be better like this:
table.insert(files2down, line)
Matt21 #4
Posted 03 February 2013 - 10:05 AM
in the function readList() you define the files2down table and in the downloadFiles() and WIMain() functions you access it. if you run downloadFiles() or WIMain() before you have run downloadFiles() then files2down is still nil and will error. I would add
local files2down={}
at the top of the program
but I don't run downloadFiles() or WIMain() before downloadFiles() :/ This is the problem :(/>
EDIT: I run WIMain() before downloadFiles(), but I use files2down after readList() in WIMain()

Also:
files2down[#files2down + 1] = line
would be better like this:
table.insert(files2down, line)
I prefer

files2down[#files2down + 1] = line
than

table.insert(files2down, line)
But table.insert(files2down, line) doing the same thing?
ChunLing #5
Posted 03 February 2013 - 09:04 PM
Yes.
Matt21 #6
Posted 03 February 2013 - 09:26 PM
:/ Mysterious error…
theoriginalbit #7
Posted 03 February 2013 - 09:38 PM
I see a problem…

I was getting an error on line 38 which is
sfile.write(dFile)
I'm assuming this is line 42 for you…

So i added this before the write:
if not sfile then error("Cannot open file: "..file,2) end

With the ",2" acting as throwback, this revealed that the calling line was line 54 and it cannot open the file "MTBOs/core/" (which i can understand why it can't open a folder as a file) ;)/>
Matt21 #8
Posted 03 February 2013 - 09:51 PM
I fixed that, but now it say the same error line 50 :(/>


function clear()
term.clear()
term.setCursorPos(1,1)
end
function menuEnding()
if menuStat == 2 then menuStat = 0
elseif menuStat == -1 then menuStat = 1 end
end
function makeDirs()
fs.makeDir("MTBOs")
fs.makeDir("MTBOs/installdata")
fs.makeDir("MTBOs/core")
end
function fError(typ, msg)
clear()

if typ == "d" then
  while true do
   clear()
   print("Erreur:")
   print(msg)
   print("Merci de contacter un admin")
   sleep(99)
  end
end

end
function webInstall()

function downloadfile(url, file, name)
  print(url)
  response = http.get(url)
  if response then print (name.." get.")
  else fError("d","Impossible de télécharger le fichier: "..name) end
  dFile = response.readAll()
  response.close()
  sfile = fs.open(file..name, "w")
  sfile.write(dFile)
  sfile.close()
end

function readList()
  file = fs.open("MTBOs/installdata/list.data", "r")
  files2down = { }
  repeat
   line = file.readLine() -------------------------- I am the line 50
   files2down[#files2down + 1] = line
  until line == nil
end

function downloadFiles()
  print(files2down[1])
  downloadfile(files2down[1], "MTBOs/core/", "firstmenu")
  -------------------------------------------------------------------- LISTE A METTRE A JOUR
end

function WIMain()
  clear()
  makeDirs()
  downloadfile("http://pastebin.com/raw.php?i=daaXMkX4", "MTBOs/installdata/list.data", "list") -- Télécharger la liste
  readList()
  print(files2down[1])
  downloadFiles()
end

WIMain()

end
function diskInstall()

function copy(from, to)
  fs.copy("disk/mtbinstall/"..from, "MTBOs/"..to)
end

function copyFiles()
  copy("core/firstmenu", "core/firstmenu")
  ------------------------------------------------------------- LISTE A METTRE A JOUR
end

clear()
makeDirs()
copyFiles()

end
function startup()

vars()

menuStat = 0
repeat
  clear()
  term.setCursorPos(1, 1)
  print ("MTB Os "..ver)
  pressedOk = false
  if menuStat == 0 then
   print[[> Installation a partir d'un disque]]
   print[[    Inserez un disk d'installation dans un disk]]
   print[[    drive à coté de l'ordinateur]]
   print[[    Vous pouvez vous procurer un disque]]
   print[[    d'installation auprès d'un admin/modo]]
   print[[  Installation par internet]]
   print[[    Se connecter à internet pour télécharger tous]]
   print[[    les fichiers nécéssaires]]
   elseif menuStat == 1 then
   print[[  Installation a partir d'un disque]]
   print[[    Inserez un disk d'installation dans un disk]]
   print[[    drive à coté de l'ordinateur]]
   print[[    Vous pouvez vous procurer un disque]]
   print[[    d'installation auprès d'un admin/modo]]
   print[[> Installation par internet]]
   print[[    Se connecter à internet pour télécharger tous]]
   print[[    les fichiers nécéssaires]]
  end
  event, param1 = os.pullEvent("key")
  if param1 == 208 then menuStat = menuStat + 1
   elseif param1 == 200 then menuStat = menuStat - 1
  elseif param1 == 28 then pressedOk = true end
  menuEnding()
until pressedOk == true

if menuStat == 1 then webInstall() end

end
function vars()
ver = "1.0"
end
startup()
theoriginalbit #9
Posted 03 February 2013 - 09:52 PM
ok is line 50 for you the EMPTY line before this
function downloadFiles()

EDIT: nvm, i see the comment.

Ok so using more debugging techniques such as PRINTing of variables and looking at the files in the computer and using ERROR throwbacks I found your problem. And I must ask, did you write your downloadFile function? because the 2 times you have used it, you have used it wrong.

line 65
downloadfile("http://pastebin.com/raw.php?i=daaXMkX4", "MTBOs/installdata/list.data", "list") -- Télécharger la liste

makes a file on the computer like so

MTBOs/installdata/list.datalist

then you try to open it later (line 47) as

MTBOs/installdata/list.data

this is what is causing the issue at line 50
Edited on 03 February 2013 - 09:03 PM
Matt21 #10
Posted 03 February 2013 - 10:11 PM
Thanks you very much :D/>

It now works. :D/>