0

I am having an "Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 24 beyond bounds [0 .. 23]'" I'm not sure how to put my .m on here without it being really long. Please let me know how i can assist to get this problem solved. Thanks in advance!

code:

#import "RootViewController.h"
#import "FSDAppDelegate.h"
#import "DetailViewController.h"
@implementation RootViewController

#pragma mark -
#pragma mark Synthesizers

@synthesize mainTableView;
@synthesize contentsList;
@synthesize descArray;
@synthesize bannerImages;
@synthesize childController;
@synthesize searchResults;
@synthesize savedSearchTerm;


#pragma mark -
#pragma mark View methods

- (void)viewDidLoad
{
   NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);


    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Descriptions" ofType:@"plist"];

    NSArray *desc = [[NSArray alloc] initWithContentsOfFile:filePath];
    self.descArray = desc;
    [desc release];

    UIImage *texas = [UIImage imageNamed:@"1andtexas.jpg"];
    UIImage *cali = [UIImage imageNamed:@"2andcalifornia.jpg"];
    UIImage *ny = [UIImage imageNamed:@"3andnewyork.jpg"];
    UIImage *tmark = [UIImage imageNamed:@"1Tmark.jpg"];
    UIImage *walkie = [UIImage imageNamed:@"Walkietalkie.jpg"];
    UIImage *onekh = [UIImage imageNamed:@"Tracingpaper.jpg"];
    UIImage *onetwonine = [UIImage imageNamed:@"diffusion.jpg"];
    UIImage *greycard = [UIImage imageNamed:@"Greycard.jpg"];
    UIImage *oneeighty = [UIImage imageNamed:@"Oneeighty.jpg"];
    UIImage *two = [UIImage imageNamed:@"Two.jpg"];
    UIImage *twofourteen = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofifteen = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twosixteen = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twotwenty = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twotwentynine = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofifty = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofiftyone = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofiftytwo = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofiftythree = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofiftyfour = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofiftyfive = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofiftysix = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofiftyseven = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *twofiftyeight = [UIImage imageNamed:@"Diffusion.jpg"];
    UIImage *thirtytwohk = [UIImage imageNamed:@"3200k.jpg"];



    NSArray *banners = [[NSArray alloc] initWithObjects:
                        texas,
                        cali,
                        ny,
                        tmark,
                        walkie,
                        onekh,
                        onetwonine,
                        greycard,
                        oneeighty,
                        two,
                        twofourteen,
                        twofifteen,
                        twosixteen,
                        twotwenty,
                        twotwentynine,
                        twofifty,
                        twofiftyone,
                        twofiftytwo,
                        twofiftythree,
                        twofiftyfour,
                        twofiftyfive,
                        twofiftysix,
                        twofiftyseven,
                        twofiftyeight,
                        thirtytwohk,
                        nil];

    self.bannerImages = banners;

    [banners release];


    [super viewDidLoad];



    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
                             @"#1 or NUMBER ONE",
                             @"#2 or NUMBER TWO",
                             @"#3 or NUMBER THREE",
                             @"1 or ONE",
                             @"10-100,10-1,10-200,10-2,10-4 and 20",
                             @"1000H PAPER",
                             @"129 or HEAVY FROST",
                             @"18% GRAY or GREY CARD",
                             @"180 DEGREE RULE",
                             @"2 or TWO",
                             @"214 or FULL TOUGH SPUN",
                             @"215 or HALF TOUGH SPUN",
                             @"216 or FULL WHITE DIFFUSION",
                             @"220 or WHITE FROST",
                             @"229 or QUARTER TOUGH SPUN",
                             @"250 or HALF WHITE DIFFUSION",
                             @"251 or QUARTER WHITE DIFFUSION",
                             @"252 or EIGHTH WHITE DIFFUSION",
                             @"253 or FULL HAMPSHIRE",
                             @"254 or NEW HAMPSHIRE",
                             @"255 or HOLLYWOOD FROST",
                             @"256 or HALF HAMPSHIRE FROST",
                             @"257 or QUARTER HAMPSHIRE FROST",
                             @"258 or EIGHTH HAMPSHIRE FROST",
                             @"3200K or TUNGSTEN",
                             nil];
    [self setContentsList:array];
    [array release], array = nil;

    if ([self savedSearchTerm])
    {
        [[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]];
    }

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    [super viewWillAppear:animated];

    [[self mainTableView] reloadData];

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

#pragma mark -
#pragma mark Table view datasource methods

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];
    for(char c = 'A'; c <= 'Z'; c++) [toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];
    return toBeReturned;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    NSInteger rows;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        rows = [[self searchResults] count];
    else
        rows = [[self contentsList] count];

    return [contentsList count];
    return rows;

    NSLog(@"rows is: %d", rows);
    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

-(UITableViewCell *) tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    NSInteger row = [indexPath row];
    NSString *contentForThisRow = nil;

    if (tableView == [[self searchDisplayController] searchResultsTableView])
        contentForThisRow = [[self searchResults] objectAtIndex:row];
    else
        contentForThisRow = [[self contentsList] objectAtIndex:row];

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    [[cell textLabel] setText:contentForThisRow];

    cell.textLabel.text = [contentsList objectAtIndex:indexPath.row];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);

    return cell;
}

#pragma mark -
#pragma mark Table view delegate methods

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"Film Set Decipher";
}

-(void) tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    FSDAppDelegate *delegate = (FSDAppDelegate *)[[UIApplication sharedApplication] delegate];
    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    detailVC.banner = [bannerImages objectAtIndex:indexPath.row];
    detailVC.description = [descArray objectAtIndex:indexPath.row];
    detailVC.title = [contentsList objectAtIndex:indexPath.row];
    self.childController = detailVC;
    [delegate.navigationController pushViewController:childController animated:YES];

    [detailVC release];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    [self handleSearchForTerm:searchString];

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    return YES;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    [self setSavedSearchTerm:nil];

    [[self mainTableView] reloadData];

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
#pragma mark -
#pragma mark Memory Management

- (void)didReceiveMemoryWarning {
    NSLog(@"Memory warning!");
    [super didReceiveMemoryWarning];
}
- (void)dealloc
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    [mainTableView release]; mainTableView = nil;
    [contentsList release]; contentsList = nil;
    [descArray release]; descArray = nil;
    [bannerImages release]; bannerImages = nil;
    [childController release]; childController = nil;
    [searchResults release], searchResults = nil;
    [savedSearchTerm release], savedSearchTerm = nil;

    [super dealloc];

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

- (void)viewDidUnload {
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    self.mainTableView = nil;
    self.contentsList = nil;
    self.descArray = nil;
    self.bannerImages = nil;
    self.childController = nil;

    [super viewDidUnload];

    [self setSavedSearchTerm:[[[self searchDisplayController] searchBar] text]];

    [self setSearchResults:nil];

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);

}

- (void)handleSearchForTerm:(NSString *)searchTerm
{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

    [self setSavedSearchTerm:searchTerm];

    if ([self searchResults] == nil)
    {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [self setSearchResults:array];
        [array release], array = nil;
    }

    [[self searchResults] removeAllObjects];

    if ([[self savedSearchTerm] length] != 0)
    {
        for (NSString *currentString in [self contentsList])
        {
            if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
            {
                [[self searchResults] addObject:currentString];
            }
        }
    }

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

@end

Turns out the problem was that the images weren't all copied into the Target>Copy Bundle Resources folder, and thats what was making it crash!?!? Thanks a million!

Sandy Patel
  • 768
  • 7
  • 19
Draken182
  • 3
  • 3

2 Answers2

0

The error says that the array has 24 elements, yet you are trying to access the 25th element. So you need to post the code that is trying to access the 25th element. Or figure out why it's trying to find the 25th element in a 24 element array.

EDIT:

You should set a breakpoint in the first line or so of tableView:didSelectRowAtIndexPath:

then at the gdb> prompt type:

po descArray

OR just:

p(int) [descArray count]

OR look in the variables pane to see how many items are actually in descArray at that point. Repeat if necessary for the other two arrays.

EDIT AGAIN:

You have another issue (possibly unrelated) in our tableView:numberOfRowsInSection:

You return the contentList count all the time. The lines after that return including the NSLogs will never be reached. As soon as you hit that first return in C, you will return from that method immediately. I don't think that's your immediate problem, but it is another problem that will happen when you start to get search working.

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48
  • i ran the debugger to se what it was, and this is what popped up: – Draken182 Nov 30 '10 at 03:42
  • 0x9515116c <+0000> mov $0xc0025,%eax 0x95151171 <+0005> call 0x950f03d8 <_sysenter_trap> 0x95151176 <+0010> jae 0x95151186 <__kill+26> 0x95151178 <+0012> call 0x9515117d <__kill+17> 0x9515117d <+0017> pop %edx 0x9515117e <+0018> mov 0xb4ef767(%edx),%edx 0x95151184 <+0024> jmp *%edx 0x95151186 <+0026> ret – Draken182 Nov 30 '10 at 03:42
  • what does the stack trace look like? That will tell you where this crash is happening. Then you need to post that code so someone can help you. – Firoze Lafeer Nov 30 '10 at 03:47
  • ugh, dare i ask how i get the stack trace? – Draken182 Nov 30 '10 at 03:50
  • The stack trace is usually in the upper left of the debugger display. Or at the gdb> prompt you can enter 'bt'. Do you have 25 or more rows in your table? It looks like you do and either bannerImages, descArray or contentsList has only 24 items in it. – Firoze Lafeer Nov 30 '10 at 03:59
  • Sorry, my prev comment about where to find the stack trace was assuming you're breaking on objc_exception_throw. If not, the call stack should have been spit out right onto the console, so you can find it there. – Firoze Lafeer Nov 30 '10 at 04:11
  • k, so it's crashing in tableView:didSelectRowAtIndexPath:. I assume the code you posted is from that method (looks like it is.) So do you know how to set a conditional breakpoint in Xcode? If not just set a regular breakpoint in this method and then check the size of each of those 3 arrays. Also pls show us what your tableView:numberOfRowsInSection: looks like. – Firoze Lafeer Nov 30 '10 at 04:33
  • NSInteger row = [indexPath row]; – Draken182 Nov 30 '10 at 04:40
  • Where did you put the breakpoint? And what do you mean "it came back with"? Did you put the breakpoint in tableView:didSelectRowAtIndexPath:? And did you check the size of those arrays at that point? – Firoze Lafeer Nov 30 '10 at 04:43
  • ok, I'm going to guess that descArray is the one missing the 25th element. Double check that plist. Also, different issue, but you don't want to release all those bannerImages in viewDidLoad. [UIImage imageNamed:] will return those images autoreleased already. – Firoze Lafeer Nov 30 '10 at 04:47
  • okay i added the breakpoint to the correct tableView:didSelectRowAtIndexPath and it said Uncaught Exception: -[NSCFBoolean length]: unrecognized selector sent to instance 0x7fff706df280 Stack Backtrace: The stack backtrace has been logged to the console. – Draken182 Nov 30 '10 at 04:49
  • descArray which is the Descriptions plist has 0-24 totaling 25 items all filled out – Draken182 Nov 30 '10 at 04:51
  • I'm going to edit my answer, since these comments are getting really long. – Firoze Lafeer Nov 30 '10 at 04:52
  • just removed all the image released as well – Draken182 Nov 30 '10 at 04:57
  • I added more comments to my answer. – Firoze Lafeer Nov 30 '10 at 05:01
  • (gdb) p(int) [descArray count] $1 = 25 Current language: auto; currently objective-c – Draken182 Nov 30 '10 at 05:09
0

Check number of objects in these bannerImages descArray contentsList

these arrays not having number of object equal to number of rows in the table view.

(You must check wher you prepare your arrays then tableview datasource and delegate methods properly).or put all code here if you not getting the solution.

Ishu
  • 12,797
  • 5
  • 35
  • 51