
Anonymous
Not applicable
2017-02-04
12:23 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Handling nulll while doing equals function
I have two variable and both are strings and I need to compare both variable if it match it should return true otherwise false.
As we know equals function (var1.equals(var2)) does not handle null values and it thows "null pointer exception ".In other words , both variables should have values only then it can work .If I used two equals function (==) function It is not giving expected result as we know the reason.
I am looking for that solution where I can compare null String with a value
var1="2016-01-03 11:05:07"
var2=null
1,235 Views
1 Reply

Anonymous
Not applicable
2017-02-04
01:19 PM
Author
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
use the java ternary operator.
var1 == null ? false : var1.equals(var2)
remember to think about how you want to handle the edge case where both are null as well (if that is indeed a valid scenario).
var1 == null ? false : var1.equals(var2)
remember to think about how you want to handle the edge case where both are null as well (if that is indeed a valid scenario).
1,235 Views
