{"id":8446,"date":"2023-08-01T06:54:29","date_gmt":"2023-08-01T06:54:29","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8446"},"modified":"2024-06-27T10:32:39","modified_gmt":"2024-06-27T10:32:39","slug":"null-pointer-exception-in-javascript","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/javascript\/null-pointer-exception-in-javascript","title":{"rendered":"Null Pointer Exception in JavaScript"},"content":{"rendered":"<p>In JavaScript, a <strong>Null Pointer Exception is not a built-in error or exception type.<\/strong> However, JavaScript does have an error called TypeError that can occur when you try to access properties or call methods on null or undefined values. When attempting to access properties or call methods on null or undefined, you may encounter a <strong>TypeError: Cannot read property &#8216;propertyName&#8217; of null\/undefined<\/strong> or similar error.<\/p>\n<p>Here&#8217;s an example that demonstrates a TypeError when trying to access a property on null:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let obj = null;\r\nconsole.log(obj.property); \/\/TypeError: Cannot read property 'property' of null\r\n<\/pre>\n<p>In this case, since <b>obj<\/b> is <b>null<\/b>, attempting to access the <b>property,<\/b> property results in a <b>TypeError<\/b> because null does not have any properties.<\/p>\n<p>Similarly, if you try to <b>call a method<\/b> on <b>null<\/b> or <b>undefined<\/b>, you will also encounter a <b>TypeError<\/b>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let func = null;\r\nfunc(); \/\/ TypeError: func is not a function\r\n\r\nlet value = undefined;\r\nvalue(); \/\/ TypeError: value is not a function\r\n<\/pre>\n<p>In these examples,<b> func and value<\/b> are set to <b>null and undefined<\/b> respectively. When trying to invoke them as functions, a <b>TypeError<\/b> occurs because they are not valid function references.<\/p>\n<p>To handle or prevent <b>TypeError<\/b> in your JavaScript code, you can perform <b>null\/undefined checks<\/b> before accessing properties or calling methods. This can be done using conditional statements, the <b>typeof operator, or optional chaining (?. operator)<\/b> introduced in ECMAScript 2020.<br \/>\n<b>For example<\/b>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let obj = null;\r\nif (obj !== null &amp;&amp; typeof obj.property !== 'undefined') {\r\n  console.log(obj.property);\r\n} else {\r\n  console.log('obj.property is null or undefined');\r\n}\r\n<\/pre>\n<p>With <b>optional chaining<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let obj = null;\r\nconsole.log(obj?.property); \/\/ it will not gonna throw any error<\/pre>\n<p>In this updated example, a null check is performed before trying to access the <b>property<\/b>. If obj is not <b>null<\/b> and <b>obj.property<\/b> is defined, it is safe to access the property without encountering a TypeError.<\/p>\n<p>By incorporating <b>null\/undefined<\/b> checks in your code, you can handle these scenarios and prevent potential <b>TypeError<\/b> exceptions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, a Null Pointer Exception is not a built-in error or exception type. However, JavaScript does have an error called TypeError that can occur when you try to access properties or call methods on null or undefined values. When attempting to access properties or call methods on null or undefined, you may encounter a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8447,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-8446","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8446"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8446"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8446\/revisions"}],"predecessor-version":[{"id":8448,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8446\/revisions\/8448"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8447"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}