0

I am trying to compile and run a simple objective c code BUT I am doing it on Windows.I am using the GNU Step and it is extremely hard for me to debug it and understand what is going on runtime.I am a .NET developer and I always use the Debugger in Visual Studio to follow the data flow and stuf but here ...... it is realy annoying.I don't have a Mac Book so I don't have the XCode too.

Can anybody tell me what is the problem in that peace of code?It is quite simple and it would be great if someone who has a Mac could debug it for me and tell me what is wrong.

The idea of the code is that it reads out a text file line by line and then on every 3 lines of code it makes an Object of NSMutableArray and adds it to another NSMutableArray.Here it is:

The read_line function:

int read_line(FILE *in, char *buffer, size_t max)
{
  return fgets(buffer, max, in) == buffer;
}

The content of the text file:

Sophie Ellis-Bextor
71222
5.01

Inna Morales
61223
6.00

Kortez Domingues
41231
3.25

The code in the main:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    FILE *in;
        if((in = fopen("C:...\\Input.txt", "rt")) != NULL)
        {
            char line[256];

            NSMutableArray* resultArray     = [[NSMutableArray alloc] init];

            while(read_line(in, line, sizeof line))
            {
            NSString *currentLine = [[NSString alloc] initWithUTF8String:line]; 
            [resultArray addObject:currentLine];
            }

            NSMutableArray*resultObjectsArray =[[NSMutableArray alloc] init];
            NSMutableArray*tmpArray =[[NSMutableArray alloc] init];

            for(int i=0 ; i <[resultArray count];i++)
            {
                if(i%4 == 3)
                {
                    [resultObjectsArray addObject:tmpArray];
                    [tmpArray removeAllObjects];
                    NSLog(@"Here we add a new object");
                }
                else
                {
                    [tmpArray addObject:[resultArray objectAtIndex:i]];
                    NSLog(@"%@",[resultArray objectAtIndex:i]);
                }
            } 
            fclose(in);     
            NSLog(@"First object in the result Array: %@",[[resultObjectsArray objectAtIndex:0] objectAtIndex:0]);      
        }

[pool drain];

All that I can see is that on the

NSLog(@"First object in the result Array: %@",[[resultObjectsArray objectAtIndex:0] objectAtIndex:0]);

line I get the next error: Uncaught Exception NSRangeException, reason:Index 0 is out of range 0 (in 'objectAtIndex:')

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mathinvalidnik
  • 1,566
  • 10
  • 35
  • 57

1 Answers1

1

I'm assuming you accidentally left off a (blank) line at the end of your input file, since the exception you mention doesn't happen with the file literally as given. I've edited your question to reflect this. Then, assuming the file is fixed in that way:

The immediate cause of the exception is that tmpArray is empty when the final NSLog is called. The reason for that is that you reuse the same tmpArray object each time through the preceding loop; you add tmpArray to resultObjectsArray, and then clear out tmpArray and start adding additional members to it. The reason this is a problem is that array elements are added by reference, not copied; you need to either copy tmpArray each time or make a brand new temporary object.

So, when you reach the final NSLog, the first element in resultObjectsArray is the same object as tmpArray; you've just called [tmpArray removeAllObjects] in the if(i%4 == 3) conditional, so it's empty; thus the second objectAtIndex:0 raises an exception.

Now, as to why the original version of the input file (the one without the blank line at the end) does not trigger the same exception (but also doesn't function correctly): your for loop goes through line by line, and adds each line to tmpArray, until it reaches a line whose index is evenly divisible by 4, at which point it clears tmpArray so it can start adding more to it the next time. The original version of the input file you gave had 11 lines, so tmpArray wasn't cleared at the end; thus tmpArray, as well as the identical objects serving as elements of resultObjectsArray, contained the last three lines read. Since it wasn't empty, objectAtIndex:0 didn't raise an except. But at the same time, the logic was wrong, since the NSLog is supposed to be returning the first element in the array, but as it happens all the elements are references to that same tmpArray object, which contains only the last stanza of lines.

echristopherson
  • 6,974
  • 2
  • 21
  • 31