Boon Boon - 4 months ago 18
Objective-C Question

-[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance

I got an exception that says:

-[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance


Is it saying I am trying to access an
NSNull
object with a key?
Any idea what causes this and how to fix it or debug further?

Answer

The way to fix it is to not attempt objectForKeyedSubscript on an NSNull object. (I'm betting you're handling some JSON data and aren't prepared for a NULL value.)

(And apparently objectForKeyedSubscript is what the new array[x] notation translates into.)

(Note that you can test for NSNull by simply comparing with == to [NSNull null], since there's one and only one NSNull object in the app.)