I have an external module that handles all of file access for the program’s persistent data. However, when I run this code, I get a runtime error “attempt to get length of global 'WESdata” (a nil value)" on the highlighted line.
do
local t = {}
print( “fileaccess.lua has been loaded.” )
WESdata = { “1”, “#”, “#”, “#”, “#”, “#”, “F”, “Discovery”}
– Write the data
t.writeTable = function()
local path = system.pathForFile(“WESTest.txt”, system.DocumentsDirectory )
local wfile = nil
wfile = io.open( path, “w+”)
– Determine if file exists
if wfile then
for i=i, #WESData do – LINE THAT CAUSES ERROR
wfile.write(wfile, WESData[i])
end
wfile.close(wfile)
else
end
wfile = nil
end
– Read the data
This function is called by
local facc = require(“fileaccess”)
facc.writeTable()
I can’t put WESData in the writeTable function because it is needed in several other functions in the same module. Besides, I tried that and got the same error.
I get the “fileaccess.lua has been loaded” message on the console, so everything should be initialized. if the program knows WESData is global, why is it nil? How do I get around this? All of the examples of file access I have seen read and write data in the function, so they are no help.
BTW, I tried setting WESData to local and prepending the “t.” to it. None of these worked.
Any help appreciated.
Thanks