Page 1 of 1

Table index is nil

Posted: Fri Mar 26, 2021 10:41 pm
by Zimbra006
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?

Re: Table index is nil

Posted: Sat Mar 27, 2021 12:59 am
by pgimeno
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:

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
Can anybody help?
I've added [code]..[/code] tags for clarity.

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
It isn't very surprising that 'values' has no i'th element. From the code I can't fully understand your intention.