Runtime error bad Argument #1 to 'pairs' (table expected, got nil)

Hello,

I occasionally get mails from users with the following error:

Runtime Error bad argument #1 to 'pairs' (table expected, got nil)

This shows up immediately after app launch. If it happened once, it will show up every time.

However, it happens only to a few users - but they are all using Android.

Since the problem does not occur on iOS, I am wondering if there is a problem in my code, or if it’s an other problem.

Did anyone experience something similar?

Best regards!

I will need more info. Do you have a for loop on a table when you launch? 

You’re passing a nil value as the first argument to pairs().

Check the variable you’re passing in to ensure it is pointing/referencing a table.

What are you doing when your app starts?

Are you reading a settings table or something like that?  

You’re going to have to track this down on your own.

One thing you can do to get more info is to NOT remove debug.* from your builds.  That way, you should get a more meaningful error message w/ a line number and filename.

Put this in build.settings:

settings = { .. other stuff build = { neverStripDebugInfo = true, }, .. other stuff }

I think you have this issue when the value you iterate is null or isn’t an array.

Use roaminggamer solution to find where exactly the problem come from. Or if you want to don’t push any update with the same issue, verify before each pair the value

Thanks for the advise!

So far I found out, that probably the problem happens in GGData (in the GGData:load function):

local data = {} local path = system.pathForFile( path .. "/" .. id .. ".box", baseDir or system.DocumentsDirectory ) local file = io.open( path, "r" ) if not file then return end data = json.decode( file:read( "\*a" ) ) io.close( file ) [...] -- Copy all the properties across. for k, v in pairs( data ) do box[k] = v end

The question would be, why the json.decode function returns nil (happens if the file is empty or does not contain valid JSON content), especially because it only happens on Android on only very rarely. The file to read is also generated by GGData.

I will have to try to get the content of this file from an affected user.

Best regards and thank you so far!

json.decode() returns nil as an indication that there was a problem. That’s your way of detecting errors. Of course, you’ve identified reasons why: invalid JSON, file doesn’t exist. You should test that file:read() actually returns something and not nil. You should also test if json.decode() returns a table or nil.

Rob

I will need more info. Do you have a for loop on a table when you launch? 

You’re passing a nil value as the first argument to pairs().

Check the variable you’re passing in to ensure it is pointing/referencing a table.

What are you doing when your app starts?

Are you reading a settings table or something like that?  

You’re going to have to track this down on your own.

One thing you can do to get more info is to NOT remove debug.* from your builds.  That way, you should get a more meaningful error message w/ a line number and filename.

Put this in build.settings:

settings = { .. other stuff build = { neverStripDebugInfo = true, }, .. other stuff }

I think you have this issue when the value you iterate is null or isn’t an array.

Use roaminggamer solution to find where exactly the problem come from. Or if you want to don’t push any update with the same issue, verify before each pair the value

Thanks for the advise!

So far I found out, that probably the problem happens in GGData (in the GGData:load function):

local data = {} local path = system.pathForFile( path .. "/" .. id .. ".box", baseDir or system.DocumentsDirectory ) local file = io.open( path, "r" ) if not file then return end data = json.decode( file:read( "\*a" ) ) io.close( file ) [...] -- Copy all the properties across. for k, v in pairs( data ) do box[k] = v end

The question would be, why the json.decode function returns nil (happens if the file is empty or does not contain valid JSON content), especially because it only happens on Android on only very rarely. The file to read is also generated by GGData.

I will have to try to get the content of this file from an affected user.

Best regards and thank you so far!

json.decode() returns nil as an indication that there was a problem. That’s your way of detecting errors. Of course, you’ve identified reasons why: invalid JSON, file doesn’t exist. You should test that file:read() actually returns something and not nil. You should also test if json.decode() returns a table or nil.

Rob