Atul Bhatia
Atul Bhatia

Reputation: 1795

ios app crash on startup

I started using crashlytics for my iphone app, and it seems that it crashes for some users during startup (or what I am guessing is startup). Here is the stack trace:

Fatal Exception: NSInvalidArgumentException
-[NSNull rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x19687eba0
Thread : Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x00000001854aa59c __exceptionPreprocess + 132
1  libobjc.A.dylib                0x0000000195bfc0e4 objc_exception_throw + 60
2  CoreFoundation                 0x00000001854b1664 __methodDescriptionForSelector
3  CoreFoundation                 0x00000001854ae418 ___forwarding___ + 928
4  CoreFoundation                 0x00000001853b2b6c _CF_forwarding_prep_0 + 92
5  UIKit                          0x0000000189fc38a0 -[UILabel _contentInsetsFromFonts] +    104
6  UIKit                          0x000000018a13ac90 -[_UILabelLayer updateContentInsets] + 76
7  UIKit                          0x000000018a13ad50 -[_UILabelLayer updateContentLayerSize] + 56
8  UIKit                          0x000000018a13ae34 -[_UILabelLayer layoutSublayers] + 36
9  QuartzCore                     0x00000001895ac564    CA::Layer::layout_if_needed(CA::Transaction*) + 320
10 QuartzCore                     0x00000001895ac408 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
11 QuartzCore                     0x00000001895abc08 CA::Context::commit_transaction(CA::Transaction*) + 276
12 QuartzCore                     0x00000001895ab98c CA::Transaction::commit() + 436
13 QuartzCore                     0x00000001895a53bc CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 80
14 CoreFoundation                 0x0000000185462a50 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
15 CoreFoundation                 0x000000018545f9dc __CFRunLoopDoObservers + 360
16 CoreFoundation                 0x000000018545fdbc __CFRunLoopRun + 836
17 CoreFoundation                 0x000000018538d0a4 CFRunLoopRunSpecific + 396
18 GraphicsServices               0x000000018e52f5a4 GSEventRunModal + 168
19 UIKit                          0x0000000189cc23c0 UIApplicationMain + 1488
20 partybutton                    0x00000001000d6de0 main (main.m:16)
21 libdyld.dylib                  0x000000019626aa08 start + 4

Not really sure how to debug this since I cannot reproduce on my own. Any pointers?

Upvotes: 6

Views: 4831

Answers (6)

praveen kumar
praveen kumar

Reputation: 9

simple way is to check with NSString *city= set value from json ; for example

then make a check -->

NSString *city=[responseDictionaryForEachRow     valueForKey:@"City"];
if (![city isKindOfClass:[NSNull class]]){
    @"Your code if string is not null";`
}

Upvotes: 0

Swap-IOS-Android
Swap-IOS-Android

Reputation: 4383

IOS does not consider NULL as nil and at run time it crashes.. To overcome this problem just simply compare your json string to NSNULL and then assign to string variable

NSString *yourVariable = ([stringVariable isEqual:[NSNull null]]) ? @"" : stringVariable;

Upvotes: 0

Lucas
Lucas

Reputation: 6729

In my case, the [NSNull rangeOfCharacterFromSet:] error was related to an empty User Defined Runtime Attributed which one of my view controllers needed to work with a third party custom Navigation Controller

Upvotes: 0

Pablo A.
Pablo A.

Reputation: 2052

kazi.munshimun, you can use a NSDictionary extension class for doing so. It will automatically check for NULL fields.

GitHub: NSDictionary extension class

Upvotes: 0

kazi.munshimun
kazi.munshimun

Reputation: 2428

I have similar kind of problem. JSON response from sever contains NULL/null. Something like this:

"result":[
    {
        "id":"44",
        "message":null,
    }]

I created a category of NSNull class. Then added the methods of NSString that causing the exceptions.

NSNull+JSON.h file:

#import <UIKit/UIKit.h>

@interface NSNull(JSON)

@end

NSNull+JSON.m file:

@implementation NSNull (JSON)

- (NSUInteger)length { return 0; }

- (NSInteger)integerValue { return 0; };

- (float)floatValue { return 0; };

- (NSString *)description { return @"0(NSNull)"; }

- (NSArray *)componentsSeparatedByString:(NSString *)separator { return @[]; }

- (id)objectForKey:(id)key { return nil; }

- (BOOL)boolValue { return NO; }

- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet{
    NSRange nullRange = {NSNotFound, 0};
    return nullRange;
}

//add methods of NSString if needed

@end

By doing this I do not have to check NULL in every variable.

Upvotes: 15

jie tang
jie tang

Reputation: 41

For my case,it is JSON data reseponed from service contain NULL String,and Json parsing treat it as string,but the iOS system doesn't.

Upvotes: 2

Related Questions