{"id":5328,"date":"2014-03-30T20:44:50","date_gmt":"2014-03-30T20:44:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/checking-for-empty-string-for-error-checking-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:44:50","modified_gmt":"2014-03-30T20:44:50","slug":"checking-for-empty-string-for-error-checking-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/checking-for-empty-string-for-error-checking-collection-of-common-programming-errors\/","title":{"rendered":"Checking for empty String for error checking-Collection of common programming errors"},"content":{"rendered":"<p>There are multiple possibilities where a runtime error can occur in <code>calculateHours()<\/code>:<\/p>\n<ul>\n<li><code>hourArray<\/code> is <code>null<\/code> and throws a <code>NullPointerException<\/code><\/li>\n<li>any <code>hourArray[i]<\/code> is null and throws a <code>NullPointerException<\/code><\/li>\n<li><code>hourArray[i].getText()<\/code> can not be parsed to a <code>Double<\/code> and throws a <code>NumberFormatException<\/code><\/li>\n<li>Your <code>hourArray<\/code> might contains of less than 7 elements, which throws an <code>IndexOutOfBoundsException<\/code><\/li>\n<\/ul>\n<p>Beside that, <code>hourArray[i].getText() != \"\"<\/code> is a bad comparison because it does not check <code>null<\/code> and checks if the two objects are the same object, not if they are equal.<\/p>\n<p>In addition, I guess you want to have <code>sum += hour<\/code> inside the loop, otherwise <code>sum<\/code> will contain the last value of the <code>hourArray<\/code>.<\/p>\n<p>So, your method should look like this:<\/p>\n<pre><code>public String calculateHours (){\n    double sum = 0;\n    if(hourArray != null){ \/\/ hourArray might be null\n        double hour = 0;\n        for (int i = 0; i &lt; hourArray.length; i++) { \/\/ use .length here\n            \/\/ check for nulls and empty String\n            if (hourArray[i] != null &amp;&amp; hourArray[i].getText() != null \n                                     &amp;&amp; !\"\".equals(hourArray[i].getText())) {\n                try{ \/\/ the text might can not be parsed to a double\n                    hour = Double.parseDouble(hourArray[i].getText());\n                }catch(NumberFormatException ex){\n                    hour = 0;\n                }\n            }\n            else  {\n                hour = 0;\n            }\n            sum += hour; \/\/ I guess you want that inside your loop\n        }\n    }\n    return String.format(\"%.2f\", sum);\n}\n<\/code><\/pre>\n<p>Anyway, it would be better if this class is written in a way that it does not have to check for all this possibilities in the <code>calculateHours()<\/code> method. You notice how hard it becomes to read if all these checks must be done here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are multiple possibilities where a runtime error can occur in calculateHours(): hourArray is null and throws a NullPointerException any hourArray[i] is null and throws a NullPointerException hourArray[i].getText() can not be parsed to a Double and throws a NumberFormatException Your hourArray might contains of less than 7 elements, which throws an IndexOutOfBoundsException Beside that, hourArray[i].getText() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5328","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5328"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5328\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}