0

On line that is marked with a comment "RIGHT HERE" (the last if statement) compiler tells me "index 0 beyond bounds for empty array" which I interpret as - the array wasn't created.

The idea is - in that last loop I'm going to sum up areas of already existing triangles with calc areas.

NSMutableArray *xCoordinate = [NSMutableArray array];
NSMutableArray *yCoordinate = [NSMutableArray array];
// some code in here...
int t;
int g = [xCoordinate count];
if (g<3) {
 printf("Please enter at least 3 value pairs to form a polygon\n");
 return 0;
}
NSMutableArray *arrayOfCorners = [NSMutableArray array];
NSMutableArray *arrayOfTriangls = [NSMutableArray array];
for (t=0; t < g; t++) {
 float x = [[xCoordinate objectAtIndex:t] floatValue];
 float y = [[yCoordinate objectAtIndex:t] floatValue];
 RectangleCorner *corner = [[RectangleCorner alloc] initWithX:x andY:y];
 // 3. add this corner to an array.
 [arrayOfCorners addObject:corner];
 if (t>=2) {
 // 4. forming a triangle.
 Triangle *triangle = [[Triangle alloc] init];
 // 5. calc its sides length. Calculate lengths and assignes those values to side1, side2, side3 properties of the triangle.
 [triangle sideLengthWithVert:arrayOfCorners[t] vert2:arrayOfCorners[t+1] vert3:arrayOfCorners[t+2]];
 // 6. calc triangle area.
 [triangle calcArea];
 // 7. adding this triangle's area to our array
 [arrayOfTriangls addObject:triangle];
 }
 // 8. adding up areas of triangles (if we have an array of them)
 int i = 0;
 NSInteger nsi = (NSInteger) i;
 // RIGHT HERE.
 Triangle *testingTriangle = [arrayOfTriangls objectAtIndex:nsi];
 if (testingTriangle)
 {
 int y = [arrayOfTriangls count];
 int r;
 for (r=0; r<=y; r++) {
 float p;
 int q = r;
 NSInteger ndi = (NSInteger) q;
 Triangle *triangle = [arrayOfTriangls objectAtIndex:ndi];
 p +=triangle.area;
 printf("Polygon's Area is %f", p);
 }
 }
}
Guillaume Algis
11k6 gold badges50 silver badges72 bronze badges
asked Apr 28, 2015 at 20:32
2
  • 1
    Rather than creating an autoreleased NSMutableArray why not try [[NSMutableArray alloc] init]; on it instead. Also, rather than calling objectAtIndex: 0 you can call [arrayOfTriangls firstObject] on it instead for safety - returning nil and silently failing. Commented Apr 28, 2015 at 20:45
  • The array was created, it's just empty. That's what the exception says. Commented Apr 29, 2015 at 0:17

2 Answers 2

3

Lets walk through your code:

if (g<3) {
 printf("Please enter at least 3 value pairs to form a polygon\n");
 return 0;
}
NSMutableArray *arrayOfCorners = [NSMutableArray array];
NSMutableArray *arrayOfTriangls = [NSMutableArray array];
for (t=0; t < g; t++) {

You create array of triangles before the loop, then go with the for loop with at least g = 3.

Now lets start with t = 0, and go through the loop:

 float x = [[xCoordinate objectAtIndex:t] floatValue];
 float y = [[yCoordinate objectAtIndex:t] floatValue];
 RectangleCorner *corner = [[RectangleCorner alloc] initWithX:x andY:y];
 // 3. add this corner to an array.
 [arrayOfCorners addObject:corner];
 if (t>=2) {
 // 4. forming a triangle.
 Triangle *triangle = [[Triangle alloc] init];
 // 5. calc its sides length. Calculate lengths and assignes those values to side1, side2, side3 properties of the triangle.
 [triangle sideLengthWithVert:arrayOfCorners[t] vert2:arrayOfCorners[t+1] vert3:arrayOfCorners[t+2]];
 // 6. calc triangle area.
 [triangle calcArea];
 // 7. adding this triangle's area to our array
 [arrayOfTriangls addObject:triangle];
 }

The if is not true at this point, so you didn't add a triangle yet to the array. The triangle array contains now 0 objects. Lets move on:

 int i = 0;
 NSInteger nsi = (NSInteger) i;
 // RIGHT HERE.
 Triangle *testingTriangle = [arrayOfTriangls objectAtIndex:nsi];

Now you try to get the object at index 0, but the array contains 0 objects. I suppose for t>= 2 your code works, but for t = 0, 1 your code crashes.

answered Apr 28, 2015 at 20:49

3 Comments

can you help me with the logic? do you see any better way to enter that if condition? Or is it hard to tell with this much code?
It's hard to do it without context, but I would think if that "if statement" is really needed in the loop, since you always get the first object in the array (nsi is always 0). If not, moving it out of the loop should help.
i meant the triangle part if (#RIGHT HERE)
0

If your array was not created, arrayOfTriangls would be nil, and calling objectAtIndex: would return nil and not crash.

Instead, "index 0 beyond bounds for empty array" means that you tried to access the first element of an empty (but initialized) array.

Your problem is that your code 7. is not executed for the first two loop iteration (if t>=2), hence the empty array.

answered Apr 28, 2015 at 20:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.