did you check why it gives you an error in routine getConfigConfigDistance?
There is a reason why it gives you an error. Probably one of the variables are nil. You need to find out when the initial error occurs (i.e. when a specific function returns nil instead of something else).