{"id":42,"date":"2008-09-25T14:27:54","date_gmt":"2008-09-25T21:27:54","guid":{"rendered":"http:\/\/www.nayansavla.com\/blog\/?p=42"},"modified":"2008-09-25T14:27:54","modified_gmt":"2008-09-25T21:27:54","slug":"testing-for-null-objects","status":"publish","type":"post","link":"https:\/\/www.nayansavla.com\/blog\/?p=42","title":{"rendered":"Testing for null Objects"},"content":{"rendered":"<p>I am not sure how many Flex\/Flash programmers are aware of this fact or also you might call me to be a noob on this point but i discovered this today in AS 3.0, I just use ! to find if the object is null or not and don&#8217;t bother testing against another property within that object but sometimes there is no other way around it.<\/p>\n<p>\u00a0<\/p>\n<p>Consider the following example,<\/p>\n<p>\u00a0<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"actionscript codecolorer\"><span class=\"kw2\">var<\/span> strObj:<span class=\"kw3\">Object<\/span> = <span class=\"st0\">&quot;&quot;<\/span>;<br \/>\n<br \/>\n<span class=\"co1\">\/\/this is standard way of finding if the objects are null or not\u00a0<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/since the object is initialized\u00a0there should not be a trace<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"sy0\">!<\/span>strObj<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span>\u00a0<br \/>\n<br \/>\n\u00a0\u00a0 \u00a0<span class=\"kw3\">trace<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;this shouldn't be happening&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>But the trace statement shows up, it almost seems that Flash is treating this object as null but no if you look at the variable using the debugger the value is not null. So this isn&#8217;t the smartest way of checking if the object is null or not specially when you are dealing with strings.<\/p>\n<p>\u00a0<\/p>\n<p>Many people\u00a0recommend doing it differently, like combing it with &amp;&amp; for some property check of that object, it would look like this,<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"actionscript codecolorer\"><span class=\"kw2\">var<\/span> strObj:<span class=\"kw3\">Object<\/span> = <span class=\"st0\">&quot;&quot;<\/span>;<br \/>\n<br \/>\n<span class=\"co1\">\/\/this is standard way of finding if the objects are null or not\u00a0<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/since the object is initialized\u00a0there should not be a trace<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ofcourse since this is string i am testing for length.<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"sy0\">!<\/span>strObj\u00a0<span class=\"sy0\">&amp;&amp;<\/span>\u00a0strObj.<span class=\"kw3\">length<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span>\u00a0<br \/>\n<br \/>\n\u00a0\u00a0 \u00a0<span class=\"kw3\">trace<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;this shouldn't be happening&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Just something to keep in mind, its not always necessary.<br \/>\n\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am not sure how many Flex\/Flash programmers are aware of this fact or also you might call me to be a noob on this point but i discovered this today in AS 3.0, I just use ! to find if the object is null or not and don&#8217;t bother testing against another property within [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[70],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-as-30","tag-as-30"],"_links":{"self":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=42"}],"version-history":[{"count":0,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}