Table index is nil

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Zimbra006
Prole
Posts: 1
Joined: Fri Mar 26, 2021 10:34 pm

Table index is nil

Post 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?
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Table index is nil

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests