Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Algorhythm app.

When I was nearing the end of the Playlist Browser with Objective-C course my code got really buggy, the build will succeed but when Xcode tries running the app in the simulator I get this warning:

2015-02-23 13:49:22.645 Algorhythm[8581:2089289] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 5]' *** First throw call stack: ( 0 CoreFoundation 0x000000010aa41f35 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010a6dabb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010a93a01e -[NSArrayI objectAtIndex:] + 190 3 Algorhythm 0x000000010a1a72a0 -[Playlist initWithIndex:] + 208 4 Algorhythm 0x000000010a1a6cc4 -[PlaylistMasterViewController viewDidLoad] + 196 5 UIKit 0x000000010af53a90 -[UIViewController loadViewIfRequired] + 738 6 UIKit 0x000000010af8206b -[UINavigationController layoutViewController:] + 44 7 UIKit 0x000000010af825b5 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 216 8 UIKit 0x000000010af826b4 -[UINavigationController _startTransition:fromViewController:toViewController:] + 92 9 UIKit 0x000000010af83487 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523 10 UIKit 0x000000010af83f47 -[UINavigationController __viewWillLayoutSubviews] + 43 11 UIKit 0x000000010b0c9509 -[UILayoutContainerView layoutSubviews] + 202 12 UIKit 0x000000010aea7973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521 13 QuartzCore 0x000000010e71cde8 -[CALayer layoutSublayers] + 150 14 QuartzCore 0x000000010e711a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 15 QuartzCore 0x000000010e71187e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 16 QuartzCore 0x000000010e67f63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 17 QuartzCore 0x000000010e68074a _ZN2CA11Transaction6commitEv + 390 18 UIKit 0x000000010ae2c54d -[UIApplication _reportMainSceneUpdateFinished:] + 44 19 UIKit 0x000000010ae2d238 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2642 20 UIKit 0x000000010ae2bbf2 -[UIApplication workspaceDidEndTransaction:] + 179 21 FrontBoardServices 0x000000010d5ea2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16 22 CoreFoundation 0x000000010a97753c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK_ + 12 23 CoreFoundation 0x000000010a96d285 __CFRunLoopDoBlocks + 341 24 CoreFoundation 0x000000010a96d045 __CFRunLoopRun + 2389 25 CoreFoundation 0x000000010a96c486 CFRunLoopRunSpecific + 470 26 UIKit 0x000000010ae2b669 -[UIApplication _run] + 413 27 UIKit 0x000000010ae2e420 UIApplicationMain + 1282 28 Algorhythm 0x000000010a1a8673 main + 115 29 libdyld.dylib 0x000000010cfd1145 start + 1 30 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Sorry with it being unreadable.

If you detectives need anything else let me know.

2 Answers

Caleb, it looks from your question that the NSException was thrown due to a nil reference for a NSArray when you call [Playlist initWithIndex:]. Try going back into the code think about how the reference could be nil in the run loop.

If you have any questions, feel free to ask!

I think it might be this line of code:

 NSUInteger index = [self.playlistImageViews indexOfObject:playlistImageView];

How do you suggest I fix it? Here is all of the code from the file it came in:

 #import "PlaylistMasterViewController.h"
#import "PlaylistDetailViewController.h"
#import "Playlist.h"

@interface PlaylistMasterViewController ()

@end

@implementation PlaylistMasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    for (NSUInteger index = 0; index < self.playlistImageViews.count; index++) {

        Playlist *playlist = [[Playlist alloc] initWithIndex:index];

        UIImageView *playlistImageViews = self.playlistImageViews[index];



        playlistImageViews.image = playlist.playlistIcon;
        playlistImageViews.backgroundColor = playlist.backgroundColor;


    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqual:@"showPlaylistDetail"]) {

        UIImageView *playlistImageView = (UIImageView *) [sender view];

        if ([self.playlistImageViews containsObject:playlistImageView]) {
            NSUInteger index = [self.playlistImageViews indexOfObject:playlistImageView];

            PlaylistDetailViewController *playlistdetailcontroller = (PlaylistDetailViewController *)segue.destinationViewController;
            playlistdetailcontroller.playlist = [[Playlist alloc] initWithIndex:0];
        }
    }
}
- (IBAction)showPlaylistDetail:(id)sender {
    [self performSegueWithIdentifier:@"showPlaylistDetail" sender:sender];
}

@end

The first line of code I gave you has a warning: Unused variable 'index'.