0
votes

I have an operating JSON library which I use to load an array of tile IDs. When I double click main.lua directly from file explorer, it runs great, but when I open Corona Simulator and open my project from there or build my project and run it on my testing device, it gives me a null reference error when I attempt to use the data I loaded.

Here is the function to load a table from a JSON file:

function fileIO.loadJSONFile (fileName)
    local path = fileName
    local contents = ""
    local loadingTable = {}
    local file = io.open (path, "r")
    print (file)
    if file then
        local contents = file:read ("*a")
        loadingTable = json.decode (contents)
        io.close (file)

        return loadingTable
    end

    return nil
end

Here is the usage:

function wr:renderChunkFile (path)
    local data = fileIO.loadJSONFile (path)

    self:renderChunk (data)
end

function wr:renderChunk (data)
    local a, b = 1

    if (self.img ~= nil) then
        a = #self.img + 1
        self.img[a] = {}
    else
        self.img[1] = {}
    end

    if (self.chunks ~= nil) then
        b = #self.chunks + 1
        self.chunks[b] = display.newGroup ()
    else
        self.chunks[1] = display.newGroup ()
    end

    for i = 1, #data do  -- Y axis                       ERROR IS HERE
        self.img[a][i] = {}
        for j = 1, #data[i] do  -- Z axis
            self.img[a][i][j] = {}
            for k = 1, #data[i][j] do  -- X axis
                if (data[i + 1] ~= nil) then
                    if (data[i + 1][j][k] < self.transparentLimit) then
                        self.img[a][i][j][k] = display.newImage ("images/tiles/"..data[i][j][k]..".png", k*self.tileWidth, display.contentHeight -j*self.tileDepth - i*self.tileThickness)
                        self.chunks[b]:insert (self.img[a][i][j][k])

                    elseif(data[i + 1] == nil) then
                        self.img[a][i][j][k] = display.newImage ("images/tiles/"..data[i][j][k]..".png", k*self.tileWidth, display.contentHeight -j*self.tileDepth - i*self.tileThickness)
                        self.chunks[b]:insert (self.img[a][i][j][k])
                    end
                end
            end
        end
    end
end

When it gets to the line for i = 1, #data do it tells me it is trying to access the length of a nil field. Where did I go wrong here?

EDIT: I feel the need to give a more clear explanation of what my problem is. I am getting inconsistent results from this program. When I select main.lua in file explorer and open it with Corona Simulator, it works. When I open Corona Simulator and internally navigate to main.lua, it does not work. When I build the project and test it on my device, it does not work. What I really need is some insight into Corona's JSON library and APK internal directory structure requirements (directory nesting limits, naming restrictions, etc.). If someone thinks of something else that might cause the issue I am having, please bring it up! I am open to anything.

1
Do you see "this is where the error is"? If so, it's probably just that the path to the file is wrong, so it can't find it. To see the error from io.open(), try wrapping it with print(), as shown in the example at the top of this page: lua.org/pil/21.2.html - Mike Andrews
Sorry! Those print statements were from debugging! I have removed them. What you suggest is a good thought, but the error actually comes at the line I mention at the end of my post. My path is correct; I have checked it several times and this whole thing does work when I open main.lua directly from file explorer. I have marked the point that the error occurs in the code now. - user3014065
Yep, but data is nil at that line, the first place that it's referenced. So, I was hoping that the io.open was failing, causing fileIO.loadJSONFile to return nil. The other option is that the JSON is not valid, and json.decode is returning nil. - Mike Andrews
I wish it was that simple. :P Unfortunately, I am positive that my JSON file is valid; it works in at least one case, so there has to be another issue. I have updated my post again; hopefully this will clear up what is actually happening here. - user3014065

1 Answers

0
votes

Without seeing the entire error message and not knowing what the value of "path" is it's going to be hard to speculate. But Corona SDK uses four base directories:

system.ResourceDirectory -- Same folder as main.lua and is read-only system.DocumentsDirectory -- Your writable folder where your data lives system.CachesDirectory -- for downloaded files system.TemporaryDirectory -- for temp files.

The last three, while in the simulator are in the project's Sandbox master folder. On device who knows where the folders really are.

In your case, if your JSON file is going to be included in with your downloadble app, your .json file should be in the same folder with your main.lua (or a sub folder) and referenced in system.ResourceDirectory.