Help debugging "attempt to index field '?'"

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.

? 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.

[lua]local colors = {}

colors[1] = { 0.31,0.18, 0.31 } – etc…[/lua]

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.

[lua]print(“myLine Color”, myLine.color)

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

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.

[lua]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[/lua]

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

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.

? 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.

[lua]local colors = {}

colors[1] = { 0.31,0.18, 0.31 } – etc…[/lua]

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.

[lua]print(“myLine Color”, myLine.color)

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

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.

[lua]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[/lua]

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

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.