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.
Related
I have been trying to work through a bug for many hours in my iOS App involving rangeOfString and have finally been able to track down where my app crashes. My app sets TextViewA to the first half of my MainTextView. What if statement should be added should be added to check for nil?
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFString rangeOfString:options:range:locale:]: nil argument'
*** First throw call stack:
.
[[myViewController tvTextFirstHalf] setText:[[mainTextView.text componentsSeparatedByString:[[myViewController tvTextMiddle]text]] objectAtIndex:0]];
..............................................................................................
No memory available to program now: unsafe to call malloc
2013-01-04 14:30:58.138 Type[7279:c07] separator: (null)
2013-01-04 14:30:58.138 Type[7279:c07] mainTextView: ; layer = ; contentOffset: {0, 0}
>
2013-01-04 14:30:58.139 Type[7279:c07] mainTextView.text: TESTTTTTTTT f f.
2013-01-04 14:30:58.142 Type[7279:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSCFString rangeOfString:options:range:locale:]: nil argument'
* First throw call stack:
(0x1a5d012 0x1882e7e 0x1a5cdeb 0xf78688 0xf87d88 0xfd05df 0x165ec 0x15dc6 0x1fd7b 0x56d8d5 0x56db3d 0xf74e83 0x1a1c376 0x1a1be06 0x1a03a82 0x1a02f44 0x1a02e1b 0x221d7e3 0x221d668 0x4be65c 0x2c5d 0x2b85)
libc++abi.dylib: terminate called throwing an exception
(gdb)
This oneliners are not considered a good practice in obj-C (i know, it's arguable).
Try to break:
[[myViewController tvTextFirstHalf] setText:[[mainTextView.text componentsSeparatedByString:[[myViewController tvTextMiddle]text]] objectAtIndex:0]];
Into some more debugging-friendly code:
NSString *separator = [[myViewController tvTextMiddle]text];
NSLog (#"separator: %#", separator);
NSLog (#"mainTextView: %#",mainTextView); //is this one nil?
NSLog (#"myViewController.tvTextMiddle: %#", myViewController.tvTextMiddle); //what about this one??
NSLog (#"mainTextView.text: %#",mainTextView.text);
NSArray *components = [mainTextView.text componentsSeparatedByString:separator];
NSLog (#"we found %d components", components.count);
NSString *result = (NSString *)[components objectAtIndex:0];
NSLog (#"result should be: %#", result);
[[myViewController tvTextFirstHalf] setText:result];
The code will break ofcourse but at least you'll know where the problem lies.
You could change last four lines then in something like:
if (components.count >= 1)
{
NSString *result = (NSString *)[components objectAtIndex:0];
NSLog (#"result should be: %#", result);
[[myViewController tvTextFirstHalf] setText:result];
}
else
{
[[myViewController tvTextFirstHalf] setText:#"empty!"];
}
EDIT:
From the NSLog you posted it is obvious that your separator [[myViewController tvTextMiddle]text] is null. Note that null is not the same as empty string. It is unallocated string.
My guess is that actually your tvTextMiddle is null (or if you are using XIB files it is not properly connected to the IBOutlet).
You can check that by adding (somewhere before the breaking-line):
NSLog (#"myViewController.tvTextMiddle: %#", myViewController.tvTextMiddle);
The crash is due to this call componentsSeparatedByString.
If your textview is nil or no text then crash'll occur.
So add a condition like:
if(mainTextView.text.length != 0 && [myViewController tvTextMiddle]text].length != 0)
{
[[myViewController tvTextFirstHalf] setText:[[mainTextView.text componentsSeparatedByString:[[myViewController tvTextMiddle]text]] objectAtIndex:0]];
}
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.
I'm having this problem where I keep getting this error in my console:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
Here is my code for retrieving the data from the XML document:
CXMLDocument *worldweather = [[CXMLDocument
alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString
stringWithFormat:#"http://free.worldweatheronline.com/feed/weather.ashx?q=%#&format=xml&num_of_days=4&key=0ded69e02b171832121504",query]]
options:0 error:nil];
weathercondition = [[[worldweather nodesForXPath:#"/data/current_condition/weatherIconUrl" error:nil]
objectAtIndex:0] stringValue];
The location that Xcode is using is Williams, CA and the query sent is
Williams+CA
When I plug this into my URL, it works and I get my XML file, but why am I getting this message in my console???
This is driving me crazy. All help would be GREATLY appreciated! Thanks in advance!
Seems that you think that [worldweather nodesForXPath:#"/data/current_condition/weatherIconUrl" error:nil] is NSArray with at least one object inside the array. This is clearly not the case, and you get an error saying that there is no object at index 0, which means that array is empty.
If you are not sure there is at least one object inside an NSArray do not just call objectAtIndex:0.
You could first check how many elements are there in array and then do the work like:
NSArray *weatherArray = [worldweather nodesForXPath:#"/data/current_condition/weatherIconUrl" error:nil];
if ([weatherArray count] > 0){
weathercondition = [[weatherArray objectAtIndex:0] stringValue];
}
If you are not sure what you are getting back from the server, see what is in the array:
NSLog(#"%#", weatherArray);
or check how many elements there are:
NSLog(#"elements in Array: %i", [weatherArray count]);
One additional useful thing you can use is for example [weatherArray lastObject], this will always return the last element of the array and will return nil even if the array is empty (it will not crash like objectAtIndex:0)
I have a nethod to refresh the content of a UIScrollView. Within the ScrollView there are several UIImageViews and some UILabels.
I do clean the content of the scrollView with the following code:
for (int i = [publicationsScrollView.subviews count] -1; i>=0; i--) {
NSLog(#"Deleting SubView: %#", [[publicationsScrollView.subviews objectAtIndex:i] class]);
if ([[publicationsScrollView.subviews objectAtIndex:i] isKindOfClass:[UIButton class]] || [[publicationsScrollView.subviews objectAtIndex:i] isKindOfClass:[UIButtonLongTap class]] || [[publicationsScrollView.subviews objectAtIndex:i] isKindOfClass:[UILabel class]] || [[publicationsScrollView.subviews objectAtIndex:i] isKindOfClass:[UIImageView class]]) {
NSLog(#"Deleting SubView: %#", [[publicationsScrollView.subviews objectAtIndex:i] class]);
[[publicationsScrollView.subviews objectAtIndex:i] removeFromSuperview];
}
if ([[publicationsScrollView.subviews objectAtIndex:i] isKindOfClass:[iCarousel class]]) {
NSLog(#"carousel: View is in Scrollview");
}
}
This code ran fine for some days. Now i recieve an error during refresh in console:
refreshWorkSpace: deleting all subviews (11)
2011-11-25 14:04:52.826 CatalogService[45054:fb03] Deleting SubView: UILabel
2011-11-25 14:04:52.827 CatalogService[45054:fb03] Deleting SubView: UILabel
2011-11-25 14:04:52.839 CatalogService[45054:fb03] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 10 beyond bounds [0 .. 9]'
*** First throw call stack:
(0x1e8c052 0x231fd0a 0x1e78db8 0x6945 0x5826 0x1e8dec9 0xddc5c2 0xddc55a 0xe81b76 0xe8203f 0xe81bab 0xecfa99 0xed1407 0xe0193f 0xe01c56 0xde8384 0xddbaa9 0x28b6fa9 0x1e601c5 0x1dc5022 0x1dc390a 0x1dc2db4 0x1dc2ccb 0x28b5879 0x28b593e 0xdd9a9b 0x2e6d 0x2de5 0x1
I can not understand what's happening here. I loop through the subviews (count should give me the number of existing subviews !?). Why could the Index be out of Bounds !?
I would suspect the exception is being thrown on this line:
if ([[publicationsScrollView.subviews objectAtIndex:i] isKindOfClass:[iCarousel class]]) {
in the case where [[publicationsScrollView.subviews objectAtIndex:i] removeFromSuperview] has just executed there will no longer be an object at index 'i'. This would only happen if the last subview in the array was one of the types you delete - otherwise the access would be harmless.
To fix, you could just add a continue after the removeFromSuperview call.
Perhaps try replacing for loop with a fast enumeration, like:
for (UIView *aView in publicationsScrollview.subviews) {
.... rewrite code to reference aView instead of using objectAtIndex
}
and see if you get the same behavior.
In the first if, if it entered you remove the subview. So, you decreased the subviews count array.
After that you try again to take the subview, in the second if, and this subview was removed, that's why it's crashing.
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.