{"id":6281,"date":"2021-08-14T12:53:13","date_gmt":"2021-08-14T12:53:13","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6281"},"modified":"2021-08-14T12:53:15","modified_gmt":"2021-08-14T12:53:15","slug":"how-to-avoid-null-pointer-exception-by-using-safe-navigator-operator","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/14\/how-to-avoid-null-pointer-exception-by-using-safe-navigator-operator\/","title":{"rendered":"How to Avoid Null Pointer Exception By Using Safe Navigator Operator (?.)"},"content":{"rendered":"\n<p>In this blog, we will discuss how to Avoid Null Pointer Exception that occurred on various conditions by using the Safe Navigator Operator. In the recent update of Salesforce, Winter&#8217;21 introduced an Operator Named As <strong>Safe Navigator Operator(?.).<\/strong> <\/p>\n\n\n\n<p>Certainly, it is efficient &amp; easy to understand the syntax for Salesforce Developers, making the coding clean. In short, using this operator no need to write code for handles explicit, sequential checks for null references. In conclusion, this operator has short-circuited expressions.<\/p>\n\n\n\n<p><span style=\"text-decoration: underline;\"><strong>Syntax<\/strong><\/span><strong>: &#8211;<\/strong>\u00a0<em><strong>e.g.: &#8211; a?.b \/\/ Evaluates to: a == null ? null : a.b<\/strong><\/em><\/p>\n\n\n\n<p>If the left-hand side of the chain expression figures out to null, the right-hand side isn\u2019t classified. In conclusion, using the safe navigation operator (<samp>?.<\/samp>) in method, variable, and property chaining. To clarify, the part of the expression that isn\u2019t classified can include variable references, method references, or array expressions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explaining Operator use by elaborating piece of Code is as follow: &#8211;<\/strong><\/h4>\n\n\n\n<p>Firstly, the below example shows a single statement replacing a block of code that checks for nulls.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String> ListAccountSites = new List&lt;String>();\n\r\n\/\/Step: 1\r\nList&lt;Account> ListAccounts = &#91;SELECT Id, Name, Phone FROM Account LIMIT 10];\n\r\n\/\/Step: 2\r\nMap&lt;Id, Account> MapAccount Details= new Map&lt;Id, Account>(&#91;SELECT Id, Name, Phone FROM Account LIMIT 10]);\r\n\nfor(Account a : ListAccounts){\r\n    \/\/Step: 3\t\r\n    Account NewAccount = MapAccount.get(a.Id);\r\n    if(NewAccount!= null){\r\n    \tif(NewAccount.site != null){\r\n        \tListAccountSites.add(NewAccount.Phone);\r\n        }\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<p>Finally, the above Example by using Safe Navigator Operator (?.):-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String> ListAccountSites = new List&lt;String>();\r\n\n\/\/Step: 1\r\nList&lt;Account> ListAccounts = &#91;SELECT Id, Name, Phone FROM Account LIMIT 10];\r\n\n\/\/Step: 2\r\nMap&lt;Id, Account> MapAccount Details= new Map&lt;Id, Account>(&#91;SELECT Id, Name, Phone FROM Account LIMIT 10]);\r\n\nfor(Account a : ListAccounts){\r\n        \tListAccountSites.add(MapAccount.get(a.Id)?.Phone);\r\n} \r<\/code><\/pre>\n\n\n\n<p>Above all,\u00a0we can use this Operator with a top-level statement, SObject chaining, SOQL Queries, etc.; for more details, refer to the below references.<\/p>\n\n\n\n<p>We hope you may find this blog resourceful and helpful. If you still have concerns and need more help, please get in touch with us at <a rel=\"noreferrer noopener\" href=\"mailto:salesforce@greytrix.com\" target=\"_blank\">salesforce@greytrix.com<\/a>.<\/p>\n\n\n\n<p><strong><em>References:-<\/em><\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/langCon_apex_SafeNavigationOperator.htm\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/langCon_apex_SafeNavigationOperator.htm<\/a><\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/05\/29\/how-to-add-utility-bar-in-a-salesforce-lightning-application\/\" target=\"_blank\" rel=\"noreferrer noopener\">Adding Utility bar in a Salesforce lightning application<\/a><\/li><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/04\/12\/how-to-notify-user-about-unsaved-changes-on-the-ui-using-lightning-component\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Notify User about Unsaved Changes on the UI using Lightning Component<\/a><\/li><li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/03\/30\/how-to-use-lightning-component-in-visualforce-page\/\">How to Use <\/a><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/03\/30\/how-to-use-lightning-component-in-visualforce-page\/\" target=\"_blank\" rel=\"noreferrer noopener\">Lightning <\/a><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/03\/30\/how-to-use-lightning-component-in-visualforce-page\/\">Component in Visualforce page<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will discuss how to Avoid Null Pointer Exception that occurred on various conditions by using the Safe Navigator Operator. In the recent update of Salesforce, Winter&#8217;21 introduced an Operator Named As Safe Navigator Operator(?.). Certainly, it is efficient &amp; easy to understand the syntax for Salesforce Developers, making the coding clean.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/08\/14\/how-to-avoid-null-pointer-exception-by-using-safe-navigator-operator\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[15,1183,207,1180,1181,1184,367],"class_list":["post-6281","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-apex-development","tag-avoid-nullpointer","tag-lightning-basics","tag-null-pointer","tag-nullpointer","tag-safe-navigator-operator","tag-salesforce"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6281"}],"version-history":[{"count":2,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6281\/revisions"}],"predecessor-version":[{"id":6284,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6281\/revisions\/6284"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}