{"id":142,"date":"2020-03-11T06:49:00","date_gmt":"2020-03-11T06:49:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=142"},"modified":"2024-02-06T11:51:49","modified_gmt":"2024-02-06T16:51:49","slug":"arraylist-nullpointerexception","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/arraylist-nullpointerexception\/","title":{"rendered":"ArrayList NullPointerException"},"content":{"rendered":"\n<p>Java&#8217;s ArrayList is a great Collection that frees us from IndexOutOfBound, one type limitation, and NullPointerException issues while providing several value-added methods. Naturally, you would be shocked if you encounter a NullPointerException on an ArrayList. The only time, you would encounter this problem is when you fail to initialize the ArrayList.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\npublic class ArrayListNullExample\n{\n   ArrayList a;\n\n   public ArrayListNullExample() {}\n\n   public int getSize() {\n       return a.size();\n   }\n\n   public static void main(String&#91;] args) {\n       ArrayListNullExample a = new ArrayListNullExample();\n       System.out.println(a.getSize());\n   }\n}<\/code><\/pre>\n\n\n\n<p>This program outputs nothing and throws a NullPointerException at&nbsp;<strong>return a.size<\/strong>. To solve this problem, simply initialize as follows and the output should be 0.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\npublic class ArrayListNullExample\n{\n   ArrayList a = new ArrayList();\n\n   public ArrayListNullExample() {}\n\n   public int getSize() {\n       return a.size();\n   }\n\n   public static void main(String&#91;] args) {\n       ArrayListNullExample a = new ArrayListNullExample();\n       System.out.println(a.getSize());\n   }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java&#8217;s ArrayList is a great Collection that frees us from IndexOutOfBound, one type limitation, and NullPointerException issues while providing several value-added methods. Naturally, you would be shocked if you encounter a NullPointerException on an ArrayList. The only time, you would encounter this problem is when you fail to initialize the ArrayList. This program outputs nothing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38,76],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-programming"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/142\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}