{"id":766,"date":"2015-03-23T09:03:02","date_gmt":"2015-03-23T09:03:02","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=766"},"modified":"2015-11-13T12:46:39","modified_gmt":"2015-11-13T12:46:39","slug":"findbug-errors","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/findbug-errors\/","title":{"rendered":"Findbug Errors"},"content":{"rendered":"<p><strong>Invocation of toString on an array<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate String &#x5B;] arrActions;\r\n\r\npublic String &#x5B;] getArrActions() {\r\n  return arrActions;\r\n}\r\n\r\npublic void setArrActions(String &#x5B;] parrActions) {\r\n  this.arrActions = parrActions;\r\n}\r\n\r\nString action = null;\r\n\r\nif(form.getArrActions() != null){\r\n  action = form.getArrActions().toString;\r\n}\r\n<\/pre>\n<p><strong>Bug Code<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  action = form.getArrActions().toString;\r\n<\/pre>\n<p><strong>Fix Code<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  action = Arrays.toString(form.getArrActions());\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p><strong>Incorrect lazy initialization and Update of Static Field<\/strong><\/p>\n<p><em>Below is the Sample code trying to implement Singleton pattern without synchronized<\/em> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static Object getInstance() {\r\n    if (instance != null) {\r\n        return instance;\r\n    }\r\n\r\n    instance = new Object();\r\n    return instance;\r\n}\r\n<\/pre>\n<p> In a multi thread environment, there would be potential for your singleton to be created more than once with your current code.<\/p>\n<p> The race condition here is on the if check. On the first call, a thread will get into the if check, and will create the instance and assign it to &#8216;instance&#8217;. But there is potential for another thread to become active between the if check and the instance creation\/assignment. This thread could also pass the if check because the assignment hasn&#8217;t happened yet. Therefore, two (or more, if more threads got in) instances would be created, and your threads would have references to different objects.<\/p>\n<p>The solution for this can be in Two Ways<br \/>\n<strong>Solution 1<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate static volatile Object myLock = new Object(); \/\/ must be declared volatile\r\n\r\npublic static Object getInstance() {\r\n    if (instance == null) { \/\/ avoid sync penalty if we can\r\n        synchronized (MyCurrentClass.myLock) { \/\/ declare a private static Object to use for mutex\r\n            if (instance == null) {  \/\/ have to do this inside the sync\r\n                instance = new Object();\r\n            }\r\n        }\r\n    }\r\n\r\n    return instance;\r\n}\r\n<\/pre>\n<p>For more Details on Volatile memory refer the following Link<br \/>\nhttp:\/\/tutorials.jenkov.com\/java-concurrency\/volatile.html<\/p>\n<p><strong>Solution 2<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic synchronized static Object getInstance() {\r\n    if (instance == null) {\r\n        instance = new Object();\r\n    }\r\n\r\n    return instance;\r\n}\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>Covariant Equals method Defined<\/strong><\/p>\n<p>When equals method not properly overridden in your class then this Error is Thrown.<\/p>\n<p><i>Things to Check<\/i><br \/>\n1.The Passing parameter should be Object Type.<\/p>\n<p><i>Code for Overridding equals() method<\/i><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic boolean equals(Object other){\r\n    boolean result;\r\n    if((other == null) || (getClass() != other.getClass())){\r\n        result = false;\r\n    } \/\/ end if\r\n    else{\r\n        People otherPeople = (People)other;\r\n        result = name.equals(otherPeople.name) &amp;&amp;  age == otherPeople.age;\r\n    } \/\/ end else\r\n\r\n    return result;\r\n} \/\/ end equals\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p><strong>int value cast to float and then passed to Math.round<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n public static String createFileSizeString(big size)\r\n {\r\n   if (size &lt; 1048576)\r\n   {\r\n     return (Math.round(((size * 10) \/ 1024)) \/ 10) + &quot; KB&quot;;\r\n   }\r\n }\r\n<\/pre>\n<p>Math.round was intended to be used on floating point arithmetic.The (size*10)\/1024 may or may not return float value.So we should explicitly convert to float before using math.round<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  (size * 10) \/ 1024 \r\n<\/pre>\n<p><em>should be <\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  ((float)size * 10) \/ 1024\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>Bad comparison of nonnegative value with negative constant<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n if (rows.size() &lt; 0 || rows.isEmpty()) {\r\n .\r\n .\r\n .\r\n }\r\n<\/pre>\n<p><strong>rows.size()<\/strong> can either be 0 or Positive Integer<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n if (rows.size() &lt; 0 || rows.isEmpty()) \r\n<\/pre>\n<p>Should be <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n if (rows.size() &lt;= 0 || rows.isEmpty()) \r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>the parameter is dead upon entry<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n public void TestSample(boolean status)\r\n {\r\n  .\r\n  . \r\n  .\r\n  status = true;\r\n }\r\n<\/pre>\n<p>The Parameter status is set to true and it was never used.So we can simply remove it as shown below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n public void TestSample(boolean status)\r\n {\r\n  .\r\n  . \r\n  .\r\n }\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>Null value guaranteed to be De referenced<\/strong><br \/>\n<em>Solution<\/em><br \/>\nCheck for null value of the Bean before calling bean method <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n .\r\n shipmentBean.getStatus();\r\n .\r\n<\/pre>\n<p>should be <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n .\r\n (shipmentBean==null?null:shipmentBean.getStatus());\r\n .\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>Suspicious reference Comparison<\/strong><br \/>\n<em>Suspicious reference Comparison of Long<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  Long val1 = 127L;\r\n  Long val2 = 127L;\r\n\r\n  System.out.println(val1 == val2);\r\n\r\n  Long val3 = 128L;\r\n  Long val4 = 128L;\r\n\r\n  System.out.println(val3 == val4);\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n true\r\n false\r\n<\/pre>\n<p>This is happening because you are comparing Long objects references, not long primitives values.<\/p>\n<p>To safely do this comparison you want (still using Long objects), you have the following options:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n System.out.println(val3.equals(val4));                    \/\/ true\r\n System.out.println(val3.longValue() == val4.longValue()); \/\/ true\r\n System.out.println((long)val3 == (long)val4);             \/\/ true\r\n<\/pre>\n<p>Java caches the primitive values from -128 to 127. When we compare two Long objects java internally type cast it to primitive value and compare it. But above 127 the Long object will not get type caste. Java caches the output by .valueOf() method.<\/p>\n<p>This caching works for Byte, Short, Long from -128 to 127. For Integer caching works From -128 to java.lang.Integer.IntegerCache.high or 127.<\/p>\n<p>Comparing non-primitives (aka Objects) in Java with == compares their reference instead of their values. Long is a class and thus Long values are Objects.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p><strong>Convert double to BigDecimal and set BigDecimal Precision<\/strong><\/p>\n<p><em> new BigDecimal(0.1)<\/em><\/p>\n<p>The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. <\/p>\n<p>This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n new BigDecimal(0.1)\r\n<\/pre>\n<p>should be<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n BigDecimal.valueOf(0.1)\r\n<\/pre>\n<p>Value that is returned by BigDecimal.valueOf is equal to that resulting from invocation of <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nDouble.toString(double).\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>Dead store to Local Variable<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Foo\r\n{\r\n    public static void Bar()\r\n    {\r\n    \r\n    }\r\n}\r\n\r\npublic class Abc\r\n{\r\n    public void Test()\r\n    {\r\n      Foo objFoo = new Foo(); \r\n      objFoo.Bar();\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Bug<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    Foo objFoo = new Foo(); \r\n    objFoo.Bar(); \r\n<\/pre>\n<p><strong>Fix<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n   Foo.Bar();\r\n<\/pre>\n<p>If I look at code someVariable.SomeMethod() I expect it to use the value of someVariable. If SomeMethod() is a static method, that expectation is invalid. Java won&#8217;t let you use a potentially uninitialized variable to call a static method, despite the fact that the only information <em>it&#8217;s going to use is the declared type of the variable<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Invocation of toString on an array private String &#x5B;] arrActions; public String &#x5B;] getArrActions() { return arrActions; } public void setArrActions(String &#x5B;] parrActions) { this.arrActions = parrActions; } String action = null; if(form.getArrActions() != null){ action = form.getArrActions().toString; } Bug Code action = form.getArrActions().toString; Fix Code action = Arrays.toString(form.getArrActions()); &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Incorrect lazy initialization and Update&hellip; <a href=\"https:\/\/codethataint.com\/blog\/findbug-errors\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[147],"tags":[],"class_list":["post-766","post","type-post","status-publish","format-standard","hentry","category-findbug"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=766"}],"version-history":[{"count":20,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/766\/revisions"}],"predecessor-version":[{"id":1041,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/766\/revisions\/1041"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}