Error Capture

Hi All,

I have a recurrent problem in which I keep making errors, mainly logistic, but sometimes technical. But, I don’t seem to know how to resolve some of them. Like the following - I have an array which keeps throwing up errors in use or in trying to print out the array. One of the elements in the array must be duff - but I can’t find which one.

Here is an example of the array, trying to set up a 3D matrix. Is there any way in which we could get Codea to run through the procedure and print out to a file/screen so that I could identify the element. Instead I get the printout:

main.lua:27: attempt to concatenate field ‘?’ (a nil value) - which is not very helpful - I need to trap the error.

function setup()
	--
	points = { x = {}, y = {}, z = {}}
	radius = 100
	antorad = math.pi/180
	index = 1
	for i = 0, 360, 30 do
		for j = 0, 360 do
			points.x[i] = radius * math.sin(i * antorad) * math.cos(j * antorad)
			points.y[i] = radius * math.sin(i * antorad) * math.sin(j * antorad)
			points.z[i] = radius * math.cos(i * antorad)
			index = index + 1
		end
	end

end

function draw()
	--
	for loop = 1, index do
		text("Array is "..loop.." x: "..points.x[loop].." y: "..points.y[loop].." z: "..points.z[loop], 100, 200+loop*20)
	end
end

May, or may not, be relevant but the code and error were taken from Love2D - which I tend to use for early trials.

Any suggestions?

Thanks.

Bri_G

:slight_smile:

There is a typo in your code: in the print() line: points.z[loop] needs to be points[loop].z

:slight_smile:

Hi @Doffer,

Don’t think that’s right - I still get the same error. Even tried points[loop].z[loop] and it still didn’t work.

I think points.z[loop] is the right syntax (or one of them), but I must admit tables are not my favourite feature of Lua.

Thanks for the feedback. I’ll keep trying and will post if I find out what the problem is.

Bri_G

[-O<

I think your main problem is that your index value ends up being far larger than the number of items in your table. If you print the value of index at the end of your setup function, its value is 4694.

So, your for loop in your draw() method will try and print out 4694 values when there are only 14. `points[3435].x for example will give you a nil value because it doesn’t exist.

There are also a number of problems with your for loop in your draw() method:

  • You start counting from 1, but earlier (in setup) you used 0 as the first index in your table. This means you won’t be printing out the first value.
  • Your for loop goes through numbers which are only one digit apart: 1, 2, 3, 4, 5… However, in your setup you used the following indexes: 0, 30, 60, 90, 120, 150…

You could just do the following:

    for loop = 0, 360, 30 do
        text("Array is "..loop.." x: "..points.x[loop].." y: "..points.y[loop].." z: "..points.z[loop], 100, 200+loop*20)
    end

@Bri_G You’re right I misread. Good luck in de debugging your code