-3

enter image description here

As seen in the image, expiryTime is printed out as null as shown in the console. However, it bypass my if condition and i even printed out (expiryTime == null) and (expiryTime == "null"). However, it is giving me as false. Why is this possible?

Ninja Dude
  • 1,332
  • 4
  • 27
  • 54
  • Your `if` is comparing against `"null"`, not `null`. It's not asking whether expiryTime is null, it's asking whether it's a string that's the same instance as the string constant `"null"`. – yshavit May 18 '17 at 15:25
  • 1
    Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. A good question does not have blurry unreadable pictures of code and error messages/stacktraces it has the actual text. –  May 18 '17 at 15:25
  • 1
    don't compare strings using `==` operator, use `String.equals(String anotherString)` or `String.equalsIgnoreCase(String anotherString)` instead – JorgeGRC May 18 '17 at 15:26
  • @yshavit It's actually comparing both, but the compare against the string `"null"` is done with `==`, not `equals()`. – Andreas May 18 '17 at 15:26
  • @Andreas - `.equals()` handles both cases, look at the source code for it. –  May 18 '17 at 15:27
  • @JarrodRoberson Yeah, I know this. What did I say that makes you think I didn't? Since the code is not calling `equals()` (which is the problem), it doesn't matter to the code how `equals()` work. --- yshavit said the code *"is comparing against "null", not null"*, and I was simply pointing out that the code is doing both, so his statement was wrong. – Andreas May 18 '17 at 15:33
  • Ah, sorry, you're right -- I was focused on the first part of the `if`, and missed the second. The change in order between the `if` and the `System.out` threw me off (my head's a bit foggy today), so I thought he was comparing against (only) `"null"` the first time, and `null` the second. (And yes, I know that `== "null"` is itself wrong -- that's why I added the "same instance" bit in my comment.) – yshavit May 18 '17 at 15:35
  • Can someone give me a workable code please thanks – Ninja Dude May 18 '17 at 15:44
  • 2
    Andreas pointed you to another SO question that describes your problem (the fact that you're comparing strings using `==`) and the fix (use the `.equals` method instead). Is that not sufficient? This is not a code-writing service, we're here to help but not to serve as a black box where broken codes goes in and fixed code comes out. – yshavit May 18 '17 at 15:46

1 Answers1

-3

Try this:

 if(expiryTime == null || expiryTime.isEmpty() || expiryTime.equals("null")) { /* do your stuffs here */ }
VK321
  • 5,768
  • 3
  • 44
  • 47
  • 1
    The "do stuff" block can never execute, since a non-null fails the first test, and null will cause NPE. Also, nothing in the question is about checking for empty string. – Andreas May 18 '17 at 15:34
  • @Desmond - try this code. It will work. – VK321 May 19 '17 at 06:59
  • @Andreas - I added empty as the author seems like confused with null and string methods. – VK321 May 19 '17 at 07:01