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

File error for No reason whatsoever

Started by Mendax, 07 December 2012 - 07:40 PM
Mendax #1
Posted 07 December 2012 - 08:40 PM
Now, I have a rather large amount of code (more than your average doorlock) and it is erroring in file.readLine(). file is defined and there is a file.close() line.
The error is:

:54: attempt to index ? (a nil value)
Here is my code.
Spoiler

tArgs = {...}
--Start of StrUtils by tomass1996 of computercraft.info
local function find(str, match, startIndex)  --Finds @match in @str optionally after @startIndex
	if not match then return nil end
	str = tostring(str)
	local _ = startIndex or 1
	local _s = nil
	local _e = nil
	local _len = match:len()
	while true do
		local _t = str:sub( _ , _len + _ - 1)
		if _t == match then
			_s = _
			_e = _ + _len - 1
			break
		end
		_ = _ + 1
		if _ > str:len() then break end
	end
	if _s == nil then return nil else return _s, _e end
end
local function seperate(str, divider)  --Separates @str on @divider
	if not divider then return nil end
	str = tostring(str)
	local start = {}
	local endS = {}
	local n=1
	repeat
		if n==1 then
			start[n], endS[n] = find(str, divider)
		else
			start[n], endS[n] = find(str, divider, endS[n-1]+1)
		end
		n=n+1
	until start[n-1]==nil
	local subs = {}
	for n=1, #start+1 do
		if n==1 then
			subs[n] = str:sub(1, start[n]-1)
		elseif n==#start+1 then
			subs[n] = str:sub(endS[n-1]+1)
		else
			subs[n] = str:sub(endS[n-1]+1, start[n]-1)
		end
	end
	return subs
end

--Now, my stuff - this is the function that reads a file into a table
function readFileLines(filename)
file = fs.open(filename,"r")
local tLines = {}
repeat
  line = file.readLine()
  table.insert(tLines,line)
until line == nil
file.close()
return tLines
end

--And this - this is the interpreter
function runFile(runfilename)
local lines = readFileLines(runfilename)
local number = 1
repeat
if lines[number] == "ECHO" then print(lines[number+1])
end
until lines[number] == nil
end
runFile(tArgs[1])
EDIT:
And the problem was: fs.open can't tell where you are. Just me not knowing something.
faubiguy #2
Posted 07 December 2012 - 08:53 PM
Attempt to index nil on a file means that the file failed to open, usually becuase it doesn't exist (or the directory doesn't exist when writing to a file).

You can check if fs.open returns nil and do something else if it does.
Lyqyd #3
Posted 07 December 2012 - 08:53 PM
What argument are you starting this program with, and if it's a valid path to a file, what is the full path of the file and what are the contents?
Mendax #4
Posted 07 December 2012 - 08:55 PM
Solved it with help from kaz on IRC.
Lyqyd #5
Posted 07 December 2012 - 08:57 PM
Please post what the issue ended up being caused by and the solution, so that others searching for help with a similar problem might have their question answered if they find this post.
Mendax #6
Posted 07 December 2012 - 10:05 PM
Ok, edited the OP.