What could cause the parameter of a function, used in a table.sort(..., function), to have a nil parameter?
In one of my sorts in TextureIt I just encountered an error "Attempt to index a nil value" and I'm not sure how it is happening. It is random, it isn't tied to any specific search. For example I've just been searching "open" & "close"
(without quotes). I just keep reloading the ui and doing the searches, sometimes both searches work, sometimes the second parameter passed to the sort function is nil.
I tried saving a reference to the table I'm sorting & examining it with /zgoo whenever the error occurs, but it looks ok to me?
Code that calls the sort & sort code:
Warning: Spoiler
Lua Code:
function TextureIt_TableSearch(text)
local startTime = GetGameTimeMilliseconds()
local sourceTable = TEXTUREIT_TEXTURES
SearchCounter = 0
local searchResults = {}
GetSearchResults(sourceTable, searchResults, text)
-- When I get an error I checked the table with /zgoo
-- to make sure contents look ok, everything looks fine.
someTempTable = searchResults
local function tableSort(tableA, tableB)
-- tableA is always ok
if not tableA then d("Not tableA") end
-- tableB is nil "sometimes"
if not tableB then d("Not tableB") end
-- error: "Attempt to index a nil value" (tableB is nil)
if tableA.text <= tableB.text then return true end
return false
end
table.sort(searchResults, tableSort)
...
end
I'm not sure if this part has anything to do with it, but I'll include it anyways. This is where the searchResults table gets populated. I pass in a destination table (searchResults) to save the matches into because of the recursion. So I don't have to copy the returned results each time the recursion happens.
Warning: Spoiler
Lua Code:
local function GetSearchResults(sourceTable, destTable, text)
local savedTextures = sourceTable
for key,value in pairs(savedTextures) do
if SearchCounter >= MAX_SEARCH_RESULTS then
return
end
if type(value) == "table" then
GetSearchResults(value, destTable, text)
elseif type(value) == "string" then
if string.lower(key):find(text) then
table.insert(destTable, {["text"] = key, ["texturePath"] = value})
SearchCounter = SearchCounter + 1
end
end
end
end
Anyone have any ideas why table.sort would pass in a nil parameter to my sort function?