Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Runtime error bad Argument #1 to 'pairs' (table expected, got nil)
Started by bjoern Jun 20 2018 04:55 AM

5 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

bjoern

[GLOBAL: userInfoPane.html]
bjoern
  • Enthusiast

  • 67 posts
  • Corona SDK

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!



[TOPIC: post.html]
#2

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 786 posts
  • Corona SDK

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



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,981 posts
  • Corona SDK

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

}



[TOPIC: post.html]
#4

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 305 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

bjoern

[GLOBAL: userInfoPane.html]
bjoern
  • Enthusiast

  • 67 posts
  • Corona SDK

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

}

 

 

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!



[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,578 posts
  • Corona Staff

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




[topic_controls]
[/topic_controls]