14
!mapData.get("PARTY_ID").equals("")         // <-- gives SonarQube error

In the above piece of code, I am getting "String literal expressions should be on the left side of an equals comparison" this error in Sonar. So how we can avoid it.

I tried this:

("").equals(!mapData.get("CON_PTY_PARTY_ID"))

But it does not work. Give some advice......

barfuin
  • 16,865
  • 10
  • 85
  • 132
Wolverine789
  • 407
  • 2
  • 6
  • 10

3 Answers3

40

Others have pointed out that the way to avoid this error is to use:

! ("".equals(mapData.get("CON_PTY_PARTY_ID")))

But no one has pointed out why this matters. The reason the literal should be on the left side of the equals comparison is to avoid the possibility of an exception if the string being compared to it is null.

As written in the question, if the value of mapData.get("CON_PTY_PARTY_ID") was null, then the expression would be trying to invoke the equals(..) method of an object that doesn't exist. That would throw an exception. By putting the literal on the left, then even if the value of mapData.get("CON_PTY_PARTY_ID") was null, the method "".equals(...) would be defined and would not throw an exception. It would simply return false.

Mark Meuer
  • 7,200
  • 6
  • 43
  • 64
2

You shouldn't surround a blank string with quotes. ("").equals(!mapData.get("CON_PTY_PARTY_ID")) should just be ! ("".equals(mapData.get("CON_PTY_PARTY_ID")))

Kevin W.
  • 1,143
  • 10
  • 15
  • Yeah, that's not what the OP intended I don't think – krodmannix Jul 09 '14 at 14:15
  • I tried with this one "".equals(!mapData.get("CON_PTY_PARTY_ID")) I am getting this error "The operator ! is undefined for the argument type(s) Object"... – Wolverine789 Jul 09 '14 at 14:21
  • Use the edited version with !("".equals(mapData.get("CON_PTY_PARTY_ID"))). – Kevin W. Jul 09 '14 at 14:24
  • Yes..I tried with this one--> !("".equals(mapData.get("CON_PTY_PARTY_ID"))). ...I am not getting any error... – Wolverine789 Jul 09 '14 at 14:31
  • But I have a doubt whether !("".equals(mapData.get("CON_PTY_PARTY_ID"))) and ("").equals(mapData.get("CON_PTY_PARTY_ID")) are same or not...I am little bit confused!!!!!!!!! – Wolverine789 Jul 09 '14 at 14:36
  • 1
    `!("".equals(mapData.get("CON_PTY_PARTY_ID")))` will be equal to `!mapData.get("PARTY_ID").equals("")`, which is what you originally had. If I were you, I would read into basic Java syntax a little more, as it seems that you're not very familiar with the language. – Kevin W. Jul 09 '14 at 14:39
1

Kevin W.'s answer is correct but has an error:

You shouldn't surround a blank string with quotes.

  ("").equals(mapData.get("CON_PTY_PARTY_ID")) 

should just be

  !("".equals(mapData.get("CON_PTY_PARTY_ID")))

The above is correct.

EDIT:

Source: https://jira.codehaus.org/browse/SONARJAVA-224

krodmannix
  • 845
  • 10
  • 30
  • I tried with this one "".equals(!mapData.get("CON_PTY_PARTY_ID")) I am getting this error "The operator ! is undefined for the argument type(s) Object"... – Wolverine789 Jul 09 '14 at 14:19
  • *Kevin W.'s answer is correct but has an error* - but both of you propose the same solution! Seems to me that Kevin W. was faster ;-) – barfuin Jul 09 '14 at 15:50
  • 2
    *You shouldn't surround a blank string with quotes.* - ... with parentheses, you mean. Quotes are ok. – barfuin Jul 09 '14 at 15:50
  • @Thomas his answer was edited after mine was posted. – krodmannix Jul 09 '14 at 16:11