Hi! I've been getting an error that I don't quite understand why is it happening
My goal is to make a csv file reader for some pictures that lead to other pictures, something like a gps, but I get this notification:
Error
Pictures.lua:38: table index is nil
Traceback
Pictures.lua:38: in function 'init'
class.lua:79: in function 'Pictures'
main.lua:30: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
My code is this:
local csvFile = love.filesystem.read('myCsv.csv')
local values = {}
local firstLine = true
while string.len(csvFile) > 0 do
local index = string.find(csvFile, "\n") or string.len(csvFile)
local line = string.sub(csvFile, 1, index - 1)
local lineValues = {}
for value in string.gmatch(line, "%w+") do
if firstLine then
table.insert(values, value)
else
table.insert(lineValues, value)
end
end
if not firstLine then
paths[lineValues[1] .. '.jpeg'] = {}
local size = #lineValues
for i = 2, size do
paths[lineValues[1] .. '.jpeg'][values] = lineValues[2] .. '.jpeg' (error line)
end
end
firstLine = false
csvFile = string.sub(csvFile, index + 1)
end
Can anybody help?
Table index is nil
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Table index is nil
I've added [code]..[/code] tags for clarity.Zimbra006 wrote: ↑Fri Mar 26, 2021 10:41 pm Hi! I've been getting an error that I don't quite understand why is it happening
My goal is to make a csv file reader for some pictures that lead to other pictures, something like a gps, but I get this notification:
Error
Pictures.lua:38: table index is nil
Traceback
Pictures.lua:38: in function 'init'
class.lua:79: in function 'Pictures'
main.lua:30: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
My code is this:
Can anybody help?Code: Select all
local csvFile = love.filesystem.read('myCsv.csv') local values = {} local firstLine = true while string.len(csvFile) > 0 do local index = string.find(csvFile, "\n") or string.len(csvFile) local line = string.sub(csvFile, 1, index - 1) local lineValues = {} for value in string.gmatch(line, "%w+") do if firstLine then table.insert(values, value) else table.insert(lineValues, value) end end if not firstLine then paths[lineValues[1] .. '.jpeg'] = {} local size = #lineValues for i = 2, size do paths[lineValues[1] .. '.jpeg'][values[i]] = lineValues[2] .. '.jpeg' (error line) end end firstLine = false csvFile = string.sub(csvFile, index + 1) end
It seems that values[i] is nil for some i within the loop. The error means that you're trying to use nil as an index to a table, in particular trying to use it to set a value. For example, this code gives the same error:
Code: Select all
t = {} ; t[nil] = 1
Who is online
Users browsing this forum: Google [Bot] and 2 guests