Getting NSDecimalNumberOverflowException Error - iphone

I'm getting an error at line: if (! [self.event.managedObjectContext save:&error]). It only happens when the user does not enter another number into the textField, so it is 0.
- (void)viewWillAppear:(BOOL)animated
{
self.textField.text = 0;
[super viewWillAppear:YES];
}
- (void)addProperty
{
NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:#"%#", self.textField.text]];
event.carPayment = decimal;
NSError *error = nil;
if (! [self.event.managedObjectContext save:&error])
{
// Handle the error.
}
NSLog(#"%#", error);
}
Error: 2011-09-21 20:31:28.101 Calculator[2391:707] Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. NSDecimalNumber overflow exception with userInfo (null)
2011-09-21 20:31:28.144 Calculator[2391:707] *** Terminating app due to uncaught exception 'NSDecimalNumberOverflowException', reason: 'NSDecimalNumber overflow exception'

I can't tell exactly what you are trying to do, but how about using try-catch syntax to handle the exception and display a message to the user that the value is not permitted to be 0. This assumes it really is not permitted to be 0 or makes no sense for it to be 0. If it does make sense for it to be 0, then try-catch is probably not what you are after.

Related

Application crashes using core data

I am using core data for saving data. First time my application saves data without any issue but 2nd time application crashes on [self.managedObjectContext save:nil]; line with crash log
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x98dda90'
*** First throw call stack:
(0x1e6e012 0x179ee7e 0x1ef94bd 0x1e5dbbc 0x1e5d94e 0xeeae92 0xf645ee 0x15ca8f5 0x15c5785 0x15c8061 0xf53dc9 0x1ec80c5 0x1e22efa 0xe88482 0x14da5f3 0x1573e5f 0x14d5a26 0x14d916e 0x77ca 0x7d4e 0x17b2705 0x3dd920 0x619b24 0x17b2705 0x3dd920 0x3dd8b8 0x49e671 0x49ebcf 0x49dd38 0x40d33f 0x40d552 0x3eb3aa 0x3dccf8 0x1dc9df9 0x1dc9ad0 0x1de3bf5 0x1de3962 0x1e14bb6 0x1e13f44 0x1e13e1b 0x1dc87e3 0x1dc8668 0x3da65c 0x3efd 0x2425)
libc++abi.dylib: terminate called throwing an exception
I am using following code for saving data
- (IBAction)save:(id)sender {
Projects *projects = [NSEntityDescription insertNewObjectForEntityForName:#"Projects" inManagedObjectContext:self.managedObjectContext];
NSLog(#"object id %#", projects.objectID);
projects.title = projectTitleTxtFld.text;
projects.descProject = descriptionTxtView.text;
projects.type = projectType.text;
projects.recID = [self generateRandom];
NSLog(#"rec id %#", projects.recID);
[self.managedObjectContext save:nil]; //here my app crashes
NSLog(#"title %#", projects.title);
}
-(NSNumber*)generateRandom
{
int number = arc4random();
if (number < 0) {
number = number * -1;
}
NSNumber *newNumber = [NSNumber numberWithInt:number];
return newNumber;
}
Its actually because of wrong data type you are trying to insert into the coredata.
From the error log what i feel is that there is some number that is assigned to the a coredata attribute which is a string.
Please have check for all the data that you are inserting using
NSLog(#"class %#",[object class]);
and check whether the core data field data type and the object data type that you are inserting are both the same
Hi I've just had this issue. I set a question to 0 % compliant on a left swipe and 100% on a right swipe. In core data i have an NSNumber data type for the attribute compliantPercentage.
i wrap a simple int number into the NSNumber using [NSNumber numberWithInt:100]
my view is a table view controller with core data hence the fetched results controller. the code snippet is:
CheckSectionQuestion *theQuestion;
theQuestion = [self.fetchedResultsController objectAtIndexPath:indexPath];
theQuestion.compliantPercentage=[NSNumber numberWithInt:100];
theQuestion.completed=[NSNumber numberWithBool:YES];
now, while this updates the table view and looks like it has updated core data, something hasn't worked. if i go back to the previous view controller using the navigation controller, then back into this view controller for ANY question - not JUST the one i have just updated i get the error you mention.
the solution?
very simple - explicitly save the changes.
ie
CheckSectionQuestion *theQuestion;
theQuestion = [self.fetchedResultsController objectAtIndexPath:indexPath];
theQuestion.compliantPercentage=[NSNumber numberWithInt:100];
theQuestion.completed=[NSNumber numberWithBool:YES];
//***** make sure CD saves the changes
NSError * error;
if ([theQuestion.managedObjectContext save:&error]) {
NSLog(#"saved");
}
hope that this helps

Unrecognized selector error processing results from geocodeAddressString

I'm trying to create multiple placemarks using MKMapItem without using coordinates.
I used location name directly in geocodeAdressString:#"Mumbai"... but I got result for single location.
While I use multiple locations through array, I'm getting this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0xab48380'
Why is this problem occurring?
Class mapItemClass=[MKMapItem class];
if(mapItemClass &&[mapItemClass respondsToSelector:#selector(openMapsWithItems:launchOptions:)])
{
NSArray *addr=[[NSArray alloc ]initWithObjects:#"Banglore",#"Mumbai",#"Delhi", nil];
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *geocodedPlacemark=[placemarks objectAtIndex:0];
MKPlacemark *placemark=[[MKPlacemark alloc]initWithCoordinate:geocodedPlacemark.location.coordinate addressDictionary:geocodedPlacemark.addressDictionary];
MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];
[MKMapItem openMapsWithItems:#[mapItem] launchOptions:nil];
}];
}
The error state that -[__NSArrayI length]: unrecognized selector sent to instance 0xab48380'
NSArray dont have a property length. So it is unable to find the selector of length. So check where you are using NSArray and keep break points to find where error is happening. length is the method of NSString and NSData but NSArray dont have length, it has count
I have been able to replicate this using this code
id array = [[NSArray alloc] initWithObjects:#"Hello", #"World", nil];
NSLog(#"%d",[array length]);
output
-[__NSArrayI length]: unrecognized selector sent to instance 0x96bafe0
notice how I have used id instead of NSArray with using id I am able to call length which isn't allowed by NSArray, but this gets round the compiler when using id.
So the best way to find out where this is going wrong is add an exception that will catch all exceptions. Do this by selecting the exceptions tab in the project navigator wind >> select '+' >> 'Add Exception Breakpoint...' >> then just select done. This will set a breakpoint every time your app throws an exception.
EDIT
Thanks to the code you have added I suspect that you are passing an NSArray where there should be an NSString. You create
NSArray *addr=[[NSArray alloc ]initWithObjects:#"Banglore",#"Mumbai",#"Delhi", nil];
then pass it to geocodeAddressString:addr
[geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
Just from the name of this parameter I suspect it should be an NSString and not an NSArray try replacing addr to [addr objectAtIndex:0] this will get the string object at index 0 of the addr array.
EDIT 2
Here is the method you are calling notice it only allows an NSString to be passed in for geocodeAddressString.
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

Terminating app due to uncaught exception 'NSRangeException' gives Range or index out of bounds'

I am working on barcode app and getting the location from CLController so its giving location,speed and many other thing so i substring that WithRange and getting exception so please kindly give me the reason and what should i do for this?
Thanks in advance.
- (void)locationUpdate:(CLLocation *)location {
locstr = [location description ];
//NSLog(#"current location is %# ",locstr);
NSString* regexString =#"<(.*?)>";
NSRegularExpressionOptions options = NSRegularExpressionCaseInsensitive;
NSError* error = NULL;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:regexString options:options error:&error];
if (error) {
//NSLog(#"error is %#", [error description]);
}
NSArray* results = [regex matchesInString:locstr options:0 range:NSMakeRange(1, [locstr length])];
for (NSTextCheckingResult* result in results) {
resultString = [locstr substringWithRange:result.range];
//NSLog(#"%#",resultString);
//life = [[resultString substringWithRange:NSMakeRange(1)] retain];
resultString =[resultString substringWithRange:NSMakeRange(1,27)];
resultString = [resultString stringByReplacingOccurrencesOfString:#" "
withString:#""];
life = [resultString stringByReplacingOccurrencesOfString:#"+"
withString:#""];
life = [[life substringWithRange:NSMakeRange(0,[life length]-1)] retain];
//NSLog(#"in update %#",life);
}
}
getting this exception
2011-11-23 14:24:58.161 BarCodeApp[2632:707] * Terminating app due
to uncaught exception 'NSRangeException', reason: '
-[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: Range or index
out of bounds' First throw call stack: (0x326398bf 0x364a31e5
0x326397b9 0x326397db 0x3783846b 0x37838a11 0x37af 0xbe73 0x3548f5df
0x3548ef81 0x3548962f 0x3260db31 0x3260d15f 0x3260c381 0x3258f4dd
0x3258f3a5 0x37cd0fed 0x30eda743 0x2337 0x22b8) terminate called
throwing an exceptionProgram received signal: “SIGABRT”. Data
Formatters temporarily unavailable, will re-try after a 'continue'.
(Can't find dlopen function, so it is not possible to load shared
libraries.)
dito Aadhira
The app could also crash at
NSMakeRange(1, [locstr length])
You define a range from 1 to [locstr length] there while [locstr length] must be [locstr length]-1.
The string "NSMakeRange" has a length of 11 characters. So, its array index range is is 0-10. Covering your code, the range would be 1-11 as you start at 1 and stop at 1+10=11.
resultString =[resultString substringWithRange:NSMakeRange(1,27)];
Are you sure the resultString length is greater than 27? By any means if it is less than 27, it will crash. It seems the app crashes there. May be in some other places also where you have used NSRange.
Enable NSZombie to get where exactly the app crashes.
Why you're parsing the [CLLocation description] instead of using the properties, like location, accuracy, etc? The description is not supposed to be used like this.

using XMLRPC with iphone giving error (please go in detail for more)?

Error I am getting:
*[NSURLError object]: unrecognized selector sent to instance 0x5f510e0 *
2010-10-11 11:41:33.718 CBH[15454:207] ** Terminating app due to uncaught exception *'NSInvalidArgumentException', reason: '-[NSURLError object]: unrecognized selector sent to instance 0x5f510e0' *
here is the code:
NSArray *args = [NSArray arrayWithObjects:#"username",#"password",nil]; // the param(s)
NSString *server = #"https://username:password#webservices.****.***:443/dsa/xmlrpc/restricted1/"; // the server
XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
[request setMethod:#"BootService.getPublicTokenGeneric" withObject:args];
id response = [self executeXMLRPCRequest:request];
[request release];
if( [response isKindOfClass:[NSError class]] ) {
NSLog(#"Error : %#",response);
}
else {
NSLog(#"Response ");
}
}
- (id)executeXMLRPCRequest:(XMLRPCRequest *)req {
NSLog(#"HEllo");
XMLRPCResponse *userInfoResponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:req];
if([userInfoResponse isKindOfClass:[NSError class]]) {
NSLog(#"Error %#",userInfoResponse);
}
return [userInfoResponse object];
}
Could some one help me tearing out this error!!
I edited the code and now I can see NSURLError is:
Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
NSErrorFailingURLStringKey=http://username:password#..org:443/zws/xmlrpc/restricted1/, NSErrorFailingURLKey=http://username:password#..org:443/zws/xmlrpc/restricted1/,
NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x5f2b300 "The network connection was lost.
Looks like XMLRPCConnection is giving you back an NSURLError object which does not like the object selector you are calling.
So userInfoResponse IS an NSURLError. You will need to test for this just like you are testing for NSError, although sooner.
XMLRPCResponse *userInfoResponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:req];
if([userInfoResponse isKindOfClass:[NSURLError class]]) {
//Do something else
}
else return [userInfoResponse object];
Your id based code is prone to these kind of errors, I suggest more strongly typed methods.

iPhone: Error and error handling confusion in comparison

NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
if(results == nil){
NSLog(#"No results found");
searchObj = nil;
}else{
if ([[[results objectAtIndex:0] name] caseInsensitiveCompare:passdTextToSearchFor] == 0) {
NSLog(#"results %#", [[results objectAtIndex:0] name]);
searchObj = [results objectAtIndex:0];
}else {
NSLog(#"No results found");
searchObj = nil;
}
}
The code above compares data a user enters to data pulled from a database. If I enter data which is in the database; it works. But if I enter complete gibberish it returns the error below instead of "No results found."
*** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSRangeException> *** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)
The results array being null should be accounted for during the checks in the above code, no?
You are potentially throwing an exception on the following line of code:
if ([[[results objectAtIndex:0] name] caseInsensitiveCompare:passdTextToSearchFor] == 0)
If the array is initialized and has zero elements, you'll pass the nil check, but you'll throw an exception when trying to access any objects within the array itself.
- (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error can return an empty array. You should use NSArray's count method instead of checking to see if the array is nil.
I recommend you set a breakpoints on objc_exception_throw and [NSException raise] as well to aid you in debugging your applications. Then run a backtrace in gdb to see where the exception is being thrown to further diagnose the real problem.
Chris Hanson has a great writeup on how to accomplish they above located here