1

I am doing some operations in for loop with nsmutablearray' s data. But sometimes it work sometimes it gives me an error like 'array index 3 empty array' at this line :

else if (min>[[enyakinarray objectAtIndex:i] floatValue]) 

full code :

for (int i=0; i<[ws3.CustomerID count]; i++) {

    //radian hesaplaması
    float total = [first floatValue];
    float theta = total * M_PI/180;
    float total2 = [second floatValue];
    float theta2 = total2 * M_PI/180;
    float total3 = [[ws3.Latitude objectAtIndex: i]  floatValue];
    float theta3 = total3 * M_PI/180;
    float total4 = [[ws3.Longitude objectAtIndex: i] floatValue];
    float theta4 = total4 * M_PI/180;


     distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin( theta3)) ;

    NSLog(@"xxxxx %f",distance);

    num = [NSNumber numberWithFloat:distance];
    [enyakinarray  addObject:num];
    NSLog(@"asasasas %@",enyakinarray);

}

float min;
NSString *s;
for (int i=0; i<[enyakinarray count]; i++) {
   if(i==0)
   { 
    min = [[enyakinarray objectAtIndex:i] floatValue];
    s= [ws3.CustomerName objectAtIndex:i];
   } 
   else if (min>[[enyakinarray objectAtIndex:i] floatValue])
            {
            min= [[enyakinarray objectAtIndex:i] floatValue];
            s = [ws3.CustomerName objectAtIndex:i];
            }
    enyakinfirma.text=s;
   }

How can I solve this?

12
  • 1
    Are you sure that ws3.CustomerName contains [enyakinarray count] objects? Commented Oct 27, 2011 at 6:40
  • What comes out from the log statement? Commented Oct 27, 2011 at 6:49
  • 2
    array index 3 empty array doesn't sound like a real error message, please post the exact message you're encountering. Commented Oct 27, 2011 at 6:50
  • yes the ws3.CustomerName's count is equal to enyakinarray' s count. Commented Oct 27, 2011 at 7:00
  • Ok I will write exact error message but application sometimes gives me sometimes works I said. The log statements are enyakinarray' s index value so distances. Commented Oct 27, 2011 at 7:02

1 Answer 1

1

This is strange but should be really easy to debug. Just write

NSLog("First array: %@", enyakinarray)
NSLog(@"Second array: %@", ws3.CustomerName)
NSLog(@"Second array: %@", ws3.CustomerID)

before your for and you should see the problem very quickly. Maybe you are changing the array contents somewhere between its creation and usage?

However, I see a design problem there. Your code uses several separate arrays CustomerName, CustomerID and enyakinarray Latitude and Longitude which contain values of a single entity Customer (name, id, latitude, longitude and distance). Maybe you could create an object Customer with these properties and mantain only one array of customers? Good OOP design helps you prevent this type of errors :)

And you can merge the two cycles into one:


float minDistance = FLT_MAX;
NSUInteger customerIndex = 0;

for (NSUInteger i = 0; i < [ws3.CustomerID count]; i++) {
    /* distance calculation code ... */

    float distance = 6371 * acos(cos(theta) * cos(theta3)
                           * cos(theta4 - theta2)
                           + sin(theta) * sin(theta3));

    if (distance < minDistance) {
        minDistance = distance;
        customerIndex = i;
    }
}

enyakinfirma.text = [ws3.CustomerName objectAtIndex:customerIndex];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.