Crash cause of [NSNull objectForKey:] but I can't find where

814 views Asked by At

I have a crash on my iPhone App, I have this error :

[724:95876] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance 0x1b5970878'

And that's all. I don't know where exactly is the crash. The project I'm on is pretty huge, is there any way to have more details on where is the call of objectForKey on a NSNull ?

2

There are 2 answers

2
Domagoj Boroš On

Do you have "All exceptions" breakpoint turned on in XCode? If not, go to the breakpoint tab, and in the "+" menu at the bottom left of the tab select Exception breakpoint. By default it should be set to catch all objc and throw exceptions. That way your code will stop exactly where the crash is happening.

3
AFTAB MUHAMMED KHAN On

Its seems you are fetching data from web-service maybe if so some of the value are coming as null. Always try to cast your response and it value according to the expected value

you can set some macro method in define header file like that or you can define near interface declaration

//formula formation

 #define NULLVALUE(m)                    ((m == nil || m==[NSNull null]) ? @"" : m)
 #define NULLVALUESpace(m)                    ((m == nil || m==[NSNull null]) ? @" " : m)
 #define NULLVALUEflaot(m)                  ((m == nil || m==[NSNull null]) ? 0.0f : (float)m)
 #define NULLVALUEcolor(m)                    ((m == nil || m==[NSNull null]) ? @"ffffff" : m)
 #define NULLVALUEOut(m)                    ((m == nil || [m length]==0) ? @"" : m)
 #define NULLArray(m)                    ((m == nil || m==null) ? new NSArray : m)
 #define SET_IF_NOT_NULL(TARGET, VAL) if(VAL != [NSNull null]) { TARGET = VAL; }

and then you can use it in anywhere in you code like

[NSString stringWithFormat:@"%@", NULLVALUESpace([Response objectForKey:key])];