attempt to index field '?' (a nil value)

Hi friends,

I cant’t understand this error, maybe because I’m new to lua.
Check this code:

[lua]local data = {}
local json = require(“json”)
local URL = “https://example.com/json

local function jsonGet(event)
if ( event.isError ) then
print ( “Network error - download failed” )
else
data = json.decode(event.response)
print("TITLE: " … data[1].title)
end
end
network.request( URL, “GET”, jsonGet )
print("TITLE: " … data[1].title)[/lua]

I’m getting this error on the second PRINT command. The first one is printing the value.

[lua]attempt to index field ‘?’ (a nil value)[/lua]

Thanks in advance [import]uid: 189638 topic_id: 33680 reply_id: 333680[/import]

The second one executes first, right?

I believe the second one executes immediately, and the data[] hasn’t been populated yet. The second print statement will only work after the network request completes (the server calls back with the data).

That’s what it looks like to me, anyways. [import]uid: 79933 topic_id: 33680 reply_id: 133888[/import]

mpappas, thank you for answering.

That is a believable explanation, but if that is true, how do I know when the network request has ended? [import]uid: 189638 topic_id: 33680 reply_id: 133893[/import]

@fac – you know when the server call has completed when the listener function is called… (That is the purpose of the listener).

You can call something else from the listener to then deal with the data it received.

Since the initial call to the server goes out over the internet, it might take a second, two (five?), for the server to answer, so you can’t just access the data immediately (so the listener should initiate the data processing action).

[import]uid: 79933 topic_id: 33680 reply_id: 133903[/import]

@mpappas is spot on. Since network.request() is an asynchronous call, that is it fires off and waits in the background, the 2nd print statement executes right away and your table data is empty.

[import]uid: 199310 topic_id: 33680 reply_id: 133948[/import]

The second one executes first, right?

I believe the second one executes immediately, and the data[] hasn’t been populated yet. The second print statement will only work after the network request completes (the server calls back with the data).

That’s what it looks like to me, anyways. [import]uid: 79933 topic_id: 33680 reply_id: 133888[/import]

mpappas, thank you for answering.

That is a believable explanation, but if that is true, how do I know when the network request has ended? [import]uid: 189638 topic_id: 33680 reply_id: 133893[/import]

@fac – you know when the server call has completed when the listener function is called… (That is the purpose of the listener).

You can call something else from the listener to then deal with the data it received.

Since the initial call to the server goes out over the internet, it might take a second, two (five?), for the server to answer, so you can’t just access the data immediately (so the listener should initiate the data processing action).

[import]uid: 79933 topic_id: 33680 reply_id: 133903[/import]

@mpappas is spot on. Since network.request() is an asynchronous call, that is it fires off and waits in the background, the 2nd print statement executes right away and your table data is empty.

[import]uid: 199310 topic_id: 33680 reply_id: 133948[/import]