2

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 ?

Mathieu Robert
  • 325
  • 1
  • 5
  • 17
  • Add an **exception breakpoint** and you will know the exact line of code which is causing the exception. – iPeter Jul 10 '18 at 14:02
  • @iPeter, I just tried it, but the breakpoint stop on 'return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));' – Mathieu Robert Jul 10 '18 at 14:06
  • Are you trying to get any value from any response dictionary or something,? It is very hard to know what exactly is the problem without much details about the problem. – iPeter Jul 10 '18 at 14:12
  • I know and that's the problem, their is many dictionary, I tested all 'objectForKey' calls in the code but none seem to crash. I did a force crash like this : NSDictionary *test = [NSNull null]; NSString *test2 = [test objectForKey:@"Test"]; It did exactly the same thing and the breakpoint stop on it, does that mean the error is somewhere in an hidden code (a pod for example) ? – Mathieu Robert Jul 10 '18 at 14:17
  • Can you paste your code here? – iPeter Jul 10 '18 at 14:21
  • Is there any call stack in the console? There should be. – Larme Jul 10 '18 at 14:25
  • As I don't know where it is I can't past the code of where is the problem – Mathieu Robert Jul 10 '18 at 14:26
  • @Larme, No, all I have is '[782:111120] -[NSNull objectForKey:]: unrecognized selector sent to instance 0x1b5970878' and the breakpoint stopping on the main autorelease – Mathieu Robert Jul 10 '18 at 14:29
  • Force a click on "continue" in the debugger? It may pops then a call stack. – Larme Jul 10 '18 at 14:41
  • @Larme, when I force continue, this show : *** First throw call stack: (0x1835fad8c 0x1827b45ec 0x183608098 0x1836005c8 0x1834e641c 0x102605d54 0x10256f0ac 0x10256f024 0x1024fa058 0x1024f9c6c 0x1024f9ba4 0x102561738 0x102560758 0x183602580 0x1834e1748 0x1840460ec 0x1835a3404 0x1835a2c2c 0x1835a079c 0x1834c0da8 0x1854a5020 0x18d4dd758 0x10243e6b0 0x182f51fc0) – Mathieu Robert Jul 10 '18 at 14:48
  • That's the call stack, but its like "non symbolized". Are you using Pods/ExternalFramework.Carthage? – Larme Jul 10 '18 at 14:51
  • The project use many Pods, but not this one – Mathieu Robert Jul 10 '18 at 14:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174735/discussion-between-mathieu-robert-and-larme). – Mathieu Robert Jul 10 '18 at 15:00

2 Answers2

2

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.

  • I did it but the code stop on 'return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));' – Mathieu Robert Jul 10 '18 at 14:21
  • Just hit the debug continue button a few more times, and you should end up with more info in the console. – koen Jul 10 '18 at 17:08
1

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])];
AFTAB MUHAMMED KHAN
  • 2,189
  • 3
  • 18
  • 24
  • 1
    I think the same think, but as for now I don't know where it is, I can't try something like this – Mathieu Robert Jul 10 '18 at 14:23
  • @MathieuRobert show me your result you are fetching from the server as this error usually come when you are fetching from server as the default declaration is nil not null so i am sure it is from server you just need to cast the response in proper way – AFTAB MUHAMMED KHAN Jul 10 '18 at 14:33
  • I would like, but it seem that this is in the framework of a Pod – Mathieu Robert Jul 10 '18 at 14:56