java - Code Violation for Equality Comparison objects with constants -


so have equality comparison as

  if( profiletypecode.equals( profileconstant.type_bill ) ) { 

this possibly generate nullpointerexception if profiletypecode not correctly contain value being compared constant.

correct

if( profileconstant.type_bill.equals( profiletypecode ) ) { 

i looking code violation plugin flag type of violations. using codepro , didn't flag this.

can me how flag kind of comparisons ?

you can use findbugs fb-contrib plugin: has diagnostic called lsc_literal_string_comparison:

this line in form of

string str = ... str.equals("someotherstring"); //or str.compareto("someotherstring"); 

a nullpointerexception may occur if string variable str null. if instead code restructured to

string str = ... "someotherstring".equals(str); //or "someotherstring".compareto(str); 

that is, call equals() or compareto() on string literal, passing variable argument, exception never happen both equals() , compareto() check null.

seems it's need.


Comments