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

Attempt to index nil with the fs api

Started by austinv11, 03 February 2014 - 08:30 PM
austinv11 #1
Posted 03 February 2014 - 09:30 PM
I created this package-creating program and i keep getting 26:attempt to index nil value, i don't understand why it doesn't work. Here's the code:

--[[
lzip is a custom file type which will self-expand
it allows users to compress any number of files into one!
]]--
local tArgs = {...}
function getInfo()
shell.run(tArgs[3].." I")
term.setTextColor(colors.yellow)
print(tArgs[3])
print("Created on computer: "..compid..", "..complabel)
print("At: "..time)
print("On: "..date)
term.setTextColor(colors.white)
end

function compress(nme)
local x = 1
if nme == nil then
  w = fs.open(tArgs[2]..".lzip", "a")
else
  w = fs.open(tArgs[2], "a")
  end
w.writeLine("local tArgs = {...}")
w.write("doc = {")
for name, data in pairs(tArgs) do
  if x > 2 then
   local r = fs.open(data, "r")
   local read = r.readAll()
   r.close()
   if nme == nil then
    local w = fs.open(tArgs[2]..".lzip", "a")
   else
    local w = fs.open(tArgs[2], "a")
    end
   if x == 3 then
    w.write(data)
    w.write(","..read)
   else
    w.write(","..data)
    w.write(","..read)
    end
   w.close()
   end
  x = x + 1
  end
if nme == nil then
  local w = fs.open(tArgs[2]..".lzip", "a")
else
  local w = fs.open(tArgs[2], "a")
  end
w.writeLine("}")
w.close()
if nme == nil then
  local w = fs.open(tArgs[2]..".lzip", "a")
else
  local w = fs.open(tArgs[2], "a")
  end
local compid = os.getComputerID()
local compla = os.getComputerLabel()
w.writeLine("local x = 1")
w.writeLine("if tArgs[1] == "..[["]].."I"..[["]].." then")
w.writeLine("compid = "..compid)
w.writeLine("complabel = "..compla)
local time = os.time()
w.writeLine("time = "..time)
local date = os.date()
w.writeLine("date = "..date)
w.writeLine("else")
w.writeLine("for name, data in pairs(doc) do")
w.writeLine("if x == 1 then")
w.writeLine("local w = fs.open(data, "..[["]].."a"..[["]]..")")
w.writeLine("if tArgs[2] ~= "..[["]].."Install"..[["]].." then")
w.writeLine("term.write("..[["]].."Expanding: "..[["]].."..".."data"..".."..[["]].."......."..[["]]..")")
w.writeLine("end")
w.writeLine("x = x + 1")
w.writeLine("else")
w.writeLine("w.write(data)")
w.writeLine("w.close()")
w.writeLine("if tArgs[2] ~= "..[["]].."Install"..[["]].." then")
w.writeLine("print("..[["]].."Success!"..[["]]..")")
w.writeLine("end")
w.writeLine("x = x - 1")
w.writeLine("end")
w.writeLine("if tArgs[2] == "..[["]].."Install"..[["]].." then")
if nme == nil then
  w.writeLine("fs.delete("..[["]]..tArgs[2]..".lzip"..[["]]..")")
else
  w.writeLine("fs.delete("..[["]]..tArgs[2]..[["]]..")")
  end
w.writeLine("end")
w.writeLine("end")
w.writeLine("end")
w.close()
end

if tArgs[1] == "compress" and tArgs[2] ~= nil and tArgs[3] ~= nil then
local cx = string.find(tArgs[2], ".lzip")
local x = 1
local e = false
if cx == nil then
  cc = fs.exists(tArgs[2]..".lzip")
else
  cc = fs.exists(tArgs[2])
  end
if cc == false then
  for name, data in pairs(tArgs) do
   if x > 2 then
    local c = fs.exists(data)
    if c == false then
	 term.setTextColor(colors.red)
	 print("Error: File "..[["]]..data..[["]].." does not exist")
	 term.setTextColor(colors.white)
	 e = true
	 end
    end
   x = x + 1
   end
  if e == false then
   compress(cx)
   end
else
  term.setTextColor(colors.red)
  if cx == nil then
   print("Error: File "..[["]]..tArgs[2]..".lzip"..[["]].." exists already!")
  else
   print("Error: File "..[["]]..tArgs[2]..[["]].." exists already!")
   end
  term.setTextColor(colors.white)
  end
elseif tArgs[1] == "get" and tArgs[2] == "info" then
local cx = string.find(tArgs[3], ".lzip")
if cx == nil then
  term.setTextColor(colors.red)
  print("Error: File "..[["]]..tArgs[3]..[["]].." is not an 'lzip'")
  term.setTextColor(colors.white)
else
  local c = fs.exists(tArgs[3])
  if c == false then
   term.setTextColor(colors.red)
   print("Error: File "..[["]]..tArgs[3]..[["]].." does not exist")
   term.setTextColor(colors.white)
  else
   getInfo()
   end
  end
else
term.setTextColor(colors.red)
print("Usages: compress <zip name> <file 2> <file 3> etc., get info <compressed file>")
term.setTextColor(colors.white)
end
Himself12794 #2
Posted 03 February 2014 - 11:17 PM
When I try to run this program, I don't get that error, I get a 39: end of stream error instead. It does seem to be creating the zipped file, although I'm fairly sure the process is not completed as I get another error when using get info on the .lzip. Not sure if all this helps you though. :/
Bomb Bloke #3
Posted 03 February 2014 - 11:26 PM
Generally, if an API you're using is erroring out, it's best to insert print statements in your own script until you're aware which particular call to that API is triggering it.

While this may have nothing to do with the issue at hand, I'm not entirely sure if this is valid:

w.writeLine("if tArgs[1] == "..[["]].."I"..[["]].." then")

Whether it is or not, use of control characters would allow you to pack it down dramatically:

w.writeLine("if tArgs[1] == \"I\" then")

It may also be worth noting that some versions of ComputerCraft do not allow file creation when using fs.open() in "append" mode.