Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Help debugging "attempt to index field '?'"
Started by granola Mar 27 2014 09:25 PM

- - - - -
2 replies to this topic

Best Answer richard9 , 28 March 2014 - 09:29 AM

? is usually just a failed trace, I'm not sure if you can rely on that for anything.

 

Anyway, so you have a big table of colors.

local colors = {}
colors[1] = { 0.31,0.18, 0.31 } -- etc...

 

That's a valid table layout, but it does mean you don't get quite-as-helpful assert messages because instead of 'index field 'blue'' you get 'index field '?'.

 

I would try adding a print statement prior to that line.

print("myLine Color", myLine.color)
print("myLine Color Table, colors[myLine.color])

 

That way, you can find out if myLine doesn't have a color number assigned (it has to be a number given your code, or things will break) or if the color table for that number doesn't exist.

 

If you just want to solve the problem but not fix the bug, I would suggest building a specific function for your color system.

local function setMyStrokeColor( obj, num )
  if num and colors[num] then 
     local colorSet = colors[num]
     myLine:setStrokeColor(colorSet[1] or 0, colorSet[2] or 0, colorSet[3] or 0)
  end
end

 

That method ensures you always set a real color value, and don't do anything if there isn't actually a color table.

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

granola

[GLOBAL: userInfoPane.html]
granola
  • Observer

  • 13 posts
  • Corona SDK

I need help debugging this error:

 

main.lua:116: attempt to index field '?' (a nil value)

stack traceback:

   main.lua: 116:in function <main.lua:99>

   ?: in function <?:218>

 

Line 116 is:

 

myLine:setStrokeColor(colors[myLine.color][1], colors[myLine.color][2], colors[myLine.color][3])

 

The colors table looks like:

 

local colors = {{0.31, 0.18, 0.31}, {0.29, 0, 0.51}, etc... } -- table of table of RGB values

 

I don't get this error all the time.  It seems to happen after I let the program run for a few minutes.  When I come back to it and interact with it, I often get this error.

 

Questions:

 

1.) Is the error saying that colors[myLine.color] is nil?

2.) what does <?:218> mean?  It looks like a line number, but I don't have that many lines in my program.



[TOPIC: post.html]
#2

richard9

[GLOBAL: userInfoPane.html]
richard9
  • Corona Geek

  • 1,118 posts
  • Enterprise

  Best Answer

? is usually just a failed trace, I'm not sure if you can rely on that for anything.

 

Anyway, so you have a big table of colors.

local colors = {}
colors[1] = { 0.31,0.18, 0.31 } -- etc...

 

That's a valid table layout, but it does mean you don't get quite-as-helpful assert messages because instead of 'index field 'blue'' you get 'index field '?'.

 

I would try adding a print statement prior to that line.

print("myLine Color", myLine.color)
print("myLine Color Table, colors[myLine.color])

 

That way, you can find out if myLine doesn't have a color number assigned (it has to be a number given your code, or things will break) or if the color table for that number doesn't exist.

 

If you just want to solve the problem but not fix the bug, I would suggest building a specific function for your color system.

local function setMyStrokeColor( obj, num )
  if num and colors[num] then 
     local colorSet = colors[num]
     myLine:setStrokeColor(colorSet[1] or 0, colorSet[2] or 0, colorSet[3] or 0)
  end
end

 

That method ensures you always set a real color value, and don't do anything if there isn't actually a color table.



[TOPIC: post.html]
#3

granola

[GLOBAL: userInfoPane.html]
granola
  • Observer

  • 13 posts
  • Corona SDK

? is usually just a failed trace, I'm not sure if you can rely on that for anything.

 

<snip>

 

Thanks.  I thought ? might mean something specific.  I added some print statements, and learned that I was calling colors[0].  My code which computed myLine.color had a hard-to-find bug which didn't always happen.




[topic_controls]
[/topic_controls]