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

This has to be the most common error I run into scripting:

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

My question: Is there a way to get more specific information on this error?

If it is fired when there is a table like this I am unsure if field a,b or c is the nil culprit…

if a[1] ~= nil then  
 if a[1].b[1] ~= nil then  
 if a[1].b[1].c[1] ~= nil then  


if #a > 0 then  
 If #a[1].b > 0 Then  

I have no real idea how you can sanitize multidimensioned arrays in an easier way.
The best thing you could to is simply populate the first array with a record value of how many records are stored in the follow up cells, in that way you will never get nil values :

a[1].b[1].c[1] = 0  
a[1].b[2].c[1] = 0  
a[1].b[3].c[1] = 0  
a[2].b[1].c[1] = 0  
a[2].b[2].c[1] = 0  
a[2].b[3].c[1] = 0  
If val ~= 0 then  
 a[1].b[1].c[2] = val  
 a[1].b[1].c[1] = a[1].b[1].c[1] + 1  
If val2 ~= 0 then  
 a[1].b[2].c[2] = val2  
 a[1].b[2].c[1] = a[1].b[2].c[1] + 1  
for _ = 2, a[1].b[2].c[1] do  

If the array in the for-loop does not contain anything on node 2, the for loop will simply not execute.

hmm, I was fearing this, would not be simple…

Thanks for the code, it`s given me something to think about though.

In the mean time I had an idea with the pcall() lua`s protected call function that seems to catch this for a bool test:

if pcall(function() local a = a[1].b[1].c[1] end) then  
 --do this as the table was valid  
 --do this as the table returned an error such as: attempt to index field '?' (a nil value)   

Here pcall() calls an anonymous function where the table in question is simply assigned to the local variable a. If the table fires no errors such as attempt to index field ‘?’ (a nil value) then pcall returns true else it returns false

It`s a bit messy but seems to work so far. Does anyone have any tips on working with pcall i.e. is it bad practice to use like this to avoid errors?

No, using pcall() as a try/catch appears to be a best practice in Lua:

Thanks Conner, I think this will save me a LOT! of headaches.

Just found this is great for housekeeping you might want to do with each reload of a tool in the global space! i.e. wont break the tool on firing up renoise but doesnt need to be linked to document create either:

--check if the song exists   
if pcall(function() local a = end) then  
-- reset global vals associated with song / tidy up changes made in renoise   

Just remember that pcalls are still evil… It is better to code right than trying to cover up mistakes that are hard to grasp…

Damn useful for debugging though…

There is also a step-trace debug tool in the library folder that you can use ;)