" NSRangeException " problem - cocoa-touch

I am trying to build an iPhone app. But at the time of compiling there is a message showing that
[Session started at 2009-01-29 18:25:40 +0600.]
2009-01-29 18:25:44.238 SimpleGame[3691:20b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (0)'
2009-01-29 18:25:44.247 SimpleGame[3691:20b] Stack: ( .........
Actually when I do active these code then this message is shown, code are:
NSMutableArray *todoArray = [[NSMutableArray alloc] init];
self.todos = todoArray;
[todoArray release];
How can I overcome this problem?

It seems like you're trying to access outside the bounds of an empty array.

put breakpoints on objc_exception_throw and [NSException raise]. This should show the error before it's actually thrown and should hopefully tell you enough to find the problem.
Tony

Related

App crashing "due to uncaught exception"

My app always crashes and goes to the main.m file. It shows the following error in the output:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'
libc++abi.dylib: terminating with uncaught exception of type NSException
How do I fix this?
This error implies that there is the function initFileURLWithPath:isDirectory: being called on a NSURL with a parameter being nil. Check your parameters that call this function and make sure they're not nil.
Hope that helps :)
This mean that [NSURL initFileURLWithPath:isDirectory:] nil so it crash.
You should put breakpoint and run again to find where is it crash. You can see image below:

String inserted into UITableView DataSource not being inserted

Whenever I insert an NSString return from a view into the UITableView's data source, when the call to insert the new cell is placed, the program crashes. Below is the code:
- (void)insertTableObject:(NSString *)thing
{
if (!locations) locations = [[NSMutableArray alloc] init];
[locations addObject:[NSString stringWithFormat:#"%#", thing]];
NSLog(#"%#", locations);
self.tableContents = [[NSMutableDictionary alloc] initWithObjectsAndKeys:locations,#"", nil];
self.sortedKeys =[[self.tableContents allKeys] sortedArrayUsingSelector:#selector(compare:)];
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:([locations count]-1) inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Below is the array printed out, and the accompanying error that takes place:
2013-03-23 15:06:32.433 XXX[9149:907] (
"D.C",
la
)
2013-03-23 15:06:32.439 XXX[9149:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x320852a3 0x39f1b97f 0x31fd0b75 0xc05bd 0x33ed854d 0x33ec9685 0x33ec5d55 0x33ec4d27 0x33ec1bf9 0x33ff6b27 0xbeceb 0xf6413 0x33f780c5 0x33f78077 0x33f78055 0x33f7790b 0x33f77e01 0x33ea05f1 0x33e8d801 0x33e8d11b 0x35b815a3 0x35b811d3 0x3205a173 0x3205a117 0x32058f99 0x31fcbebd 0x31fcbd49 0x35b802eb 0x33ee1301 0xb701b 0xb6f70)
libc++abi.dylib: terminate called throwing an exception
Is it a problem that "la" is printing without quotes? Could the system potentially not be seeing that entry?
I ended up solving the problem. I had cells with detail views, so when it would draw the detailView index, it wasn't present. I added a simple line to the function in the original post to add another value to the detailView array.

Data not loading into core data database

I'm trying to put some data in the database. I'm using the following code also in other views. There does it work. But on this view it doesn't work. You can see the code below.
- (void)fetchKlassementIntoDocument:(UIManagedDocument *)document
{
NSLog(#"log");
dispatch_queue_t fetchKlassement = dispatch_queue_create("klassement fetcher", NULL);
dispatch_async(fetchKlassement, ^{
NSLog(#"Log2");
NSLog(#"log3");
NSArray *klassement = [GenkData getRanking];
[document.managedObjectContext performBlock:^{
NSLog(#"klassement array %#",klassement);
for (NSDictionary *genkInfo in klassement ) {
[Klassement klassementWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext];
NSLog(#"inserted klassement");
}
NSLog(#"Log4");
[document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];
NSLog(#"Log5");
NSLog(#"Log 6");
}];
});
NSLog(#"Log7");
dispatch_release(fetchKlassement);
NSLog(#"Log8");
}
This gives the following error log.
2012-10-25 10:50:07.527 RacingGenk[17989:907] log
2012-10-25 10:50:07.528 RacingGenk[17989:907] Log7
2012-10-25 10:50:07.529 RacingGenk[17989:907] Log8
2012-10-25 10:50:07.531 RacingGenk[17989:4f03] Log2
2012-10-25 10:50:07.532 RacingGenk[17989:4f03] log3
2012-10-25 10:50:07.604 RacingGenk[17989:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
After fetching the data I fill up a tableview with this data. I think that's why the error came up. But can somebody say me what is going wrong ?
Kind regards.
The line from your code:
NSArray *klassement = [GenkData getRanking];
Check the getRanking method & the klassement Array. The crash is occurring due to this line only.
Put a log for both. Hope it'll resolve your issue.

*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array. When pushing to a navigation controller

I am writing an iPad app. I load a view in with "initWithNibName" and then trying to push the view onto the Navigation controller.
[self.navigationController pushViewController:tvc animated:YES];
This causes the app to crash giving the following stackTrace
2011-09-01 12:46:06.040 PuckDragDemo[4932:f803] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
* First throw call stack:
(0x1191062 0x1322d0a 0x117ddc8 0x2979 0xae95 0x449ebe 0x44a1b1 0x45bccd 0x45bebf 0x45c18b 0x45cc2f 0x45d1d6 0x45cdf4 0x13975 0x1192ed9 0x386eb2 0x386e4a 0x42c3e6 0x42c8af 0x42bb6e 0x3ac2a0 0x3ac4c6 0x392c74 0x386399 0x1d87fa9 0x11651d5 0x10ca042 0x10c892a 0x10c7dd4 0x10c7ceb 0x1d86879 0x1d8693e 0x38438b 0x1c49 0x1bc5)
terminate called throwing an exception(gdb) bt
I have no idea why I am getting this error when I don't have an array. my only guess is that there is an array in the background somewhere which is getting called?
P.S the code requested
if (self.started == YES) return;
if (self.bEditMode == YES) {
[self go];
} else {
GetReadyModalViewController *modalVC = [[GetReadyModalViewController alloc] initWithNibName:#"GetReadyModelViewController" bundle:nil];
modalVC.delegate = self;
[self presentModalViewController:modalVC animated:NO];
modalVC.taskTitle.text = [self.taskPreferences objectForKey:#"title"];
modalVC.taskDescription.text = [self.taskPreferences objectForKey:#"description"];
}
}
It is irrelevant to the pushing on the navigationController, it is a problem inside of the tic viewController. Please post the code from the viewDidLoad/viewWillAppear, as your are instantiating an array and trying to get data from it [myMutableArray objectAtIndex:**0**]
Fixed it. I don't know exactly how it worked. There is a array in my code but not directly connected to the navigation controller. It appears that a index in the array was being called that didn't exist but for some reason it wasn't calling an error. Don't ask me why but when the navigation controller pushed the view it picked up on a previous undetected error.

set file's icon in a command line utility not working

I just began to work with Objective-C and I'm managing pretty well. My last challenge was to make a command line utility, which I could than use in AppleScript. But my code does not work, not in the terminal, not in the AppleScript. So I'm asking you, what's the error in this peace of code, that should be very plain and easy?
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// -imagePath
// -filePath
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
NSString *soundPath = [args stringForKey:#"imagePath"];
NSString *filePath = [args stringForKey:#"filePath"];
BOOL worked = [[NSWorkspace sharedWorkspace] setIcon:[[NSImage alloc] initWithContentsOfFile:soundPath] forFile:filePath options:0];
NSLog(#"Worked: %i",worked);
[pool release];
return 0;
}
2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'
In effect, that means “You can't do that in a command-line tool”. If you run your tool in the debugger, it'll tell you what “that” is in the stack trace. My guess is that “that” is creating the NSImage.
Another solution is to rewrite the tool to use Icon Services instead of NSWorkspace. The APIs you'll need are still available and not deprecated.
Terminal:
macbook-van-ief2:~ ief2$ /Users/ief2/Desktop/iConChange/build/Debug/iConChange -filePath "~/Desktop/Naamloos.txt" -imagePath "~/Desktop/Z_Home_ZIcon.gif"
2010-01-31 17:03:24.311 iConChange[14848:10b] _NXCreateWindow: error setting window property (1002)
2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'
2010-01-31 17:03:24.322 iConChange[14848:10b] Stack: (
2459177131,
2487344699,
2459176587,
2459176650,
2441787103,
2441786331,
2441785537,
2441784212,
2441781861,
2441794711,
2441793509,
2441762807,
2444980701,
2444978472,
2447881218
)
Trace/BPT trap
2010-01-31 17:03:24.317 iConChange[14848:10b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'
In effect, that means “You can't do that in a command-line tool”. If you run your tool in the debugger, it'll tell you what “that” is in the stack trace.
One solution is to rewrite it as an application. Perhaps a document-based one, with an image view in the document window, set up to receive dragged images and files.
I have an application that uses basically that code to set icons on OS X:
http://pzich.com/junk/Iconizer.app.zip
I'm not sure why yours isn't working.