I'm sure it's obvious but can't figure out what I'm missing. Code is:
} else if (flag != null && date != null &&
!date.equals("") && (disability != null || flagEnd == null))
//do stuff
}
I've double checked the variables. flag is not null, date IS null, disability is null and flagEng is null. The code shouldn't be stepping into this because the date is null, and yet it is. Do I have this written incorrectly?
Note: date is a string. flagEnd is a java.util.Date. Thanks!
Edit: Here is the code that creates the date variable.
String date = (rs.getString("date") != null && rs.getString("date").length() > 0 ? rs.getString("date") : "NULL");
Instead of date != null, should I maybe instead have this?
!date.equals("NULL")
The string was actually storing the word "NULL", as opposed to being an empty string. Once I changed the code to !date.equals("NULL"), it skipped the ELSE IF statement like it should have.
Related
In java, what is the difference between this 2 lines of code, or the second line of code don't even exist
if (name != null){}
if (null != name){}
the real code is something like this
if ( null != name&& !StringUtils.isEmpty(name) )
by the way, I have tested this code and it works
There is no difference between your two top lines of code as both do a valid null check. The main issue is if you did something like this:
if (myString.isEmpty() && (myString != null)) {...}
This would throw a NullPointerException if myString is null since you're dereferencing the variable before doing the null check. Better to do instead:
if ((myString != null) && myString.isEmpty()) {...}
The && operator does a boolean AND test, and will short circuit, will end and not do the right sided test if the test on the left is false.
There is no difference, != is a Logical Operator, and is checking to make sure they are not equivalent, so it doesn't matter which side things are on. For example:
String a = "a";
if(a != null)
{
System.out.println("they are not equal");
}
if(null != a)
{
System.out.println("they are not equal");
}
returns
they are not equal
they are not equal
Difference is in that you cannot assign to NULL anything.
If you would make typo and miss a ! sign within
if (name != null) {}
You could possibly make
if (name = null) {}
And by that assign null value to name
Read this
https://knowthecode.io/yoda-conditions-yoda-not-yoda
Am checking validation of date using regex how it works for date but not for Year ? .Please help me to solve this issue.
Output :
false
true
false
true
Expected Output :
false
false
false
false
public static void main(String args[])
{
System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "08s21988"));
System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "08021s88"));
System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "s8021988"));
System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "0802198s"));
}
public static boolean dateFormatValidate(String format, String regex, String value) {
try {
if (value != null && !"".equals(value.trim()) && format != null && !"".equals(format.trim()))
{
if ((regex != null && !"".equals(regex.trim()) && Pattern.matches(regex, value)) || regex != null || "".equals(regex))
{
SimpleDateFormat dformat = new SimpleDateFormat(format);
dformat.setLenient(false);
dformat.parse(value);
return true;
} else
return false;
} else
return false;
} catch (Exception e) {
return false;
}
}
#sunleo I don't think so it has anything to do with your regex, as I have just tried your pattern on these four dates you provide and it doesn't capture any of them.
I would say the error is in this if:
if ((regex != null && !"".equals(regex.trim()) && Pattern.matches(regex, value)) || regex != null || "".equals(regex))
{
// your code
}
In the cases you provided in your main:
regex != null - all cases true
!"".equals(regex.trim()) - all cases true
Pattern.matches(regex, value)) - all cases false
regex != null - all cases true
"".equals(regex)) - all cases false
Which gives us following if:
if ( (true AND true AND false) OR true OR false )
which is the same as:
if ( false OR true OR false )
which gives in all cases:
true
So why you still managed to get two false outputs? Probably an exception was thrown here:
SimpleDateFormat dformat = new SimpleDateFormat(format);
dformat.setLenient(false);
dformat.parse(value);
So in your catch statement change return false to e.printStackTrace();.
Also my recommendation would be to rearrange this particular if first then check it for the cases that should be false (in this example all of them).
How?
First rearrange if then I would start with debugging and checking the if components to see their values and if they are computed correctly.
Also I think the regex is not correct at all (even if it's not the cause of the wrong output), if you always use ddMMyyyy format and only 19xx/20xx years, try this pattern:
^(0[1-9]|[1-2][0-9]|3[0-1])(0[1-9]|1[0-2])(19|20)\d{2}$
NOTE
I have not checked any of this (except regex) via any IDE as I do not have any here.
I am developing an android application.I am getting a String value as null from webservice. I am fetching the value and storing it in a String variable. Then when I print the value using Log, like Log.i("tag", "````!!!cell >>"+cell);, I an getting null printed in the screen. Now what i need is that I need to check the variable for 'null' and I want to display a textfield with no value if it is 'null'. I am using the following statement for checking
if(!cell.equals(null) || !cell.equals("")) {
_______
} else {
_______
}
But the control is not going inside the else part if the value us 'null'
Please give me a solution.
Thanks in advance.
when cell is null , and you are trying to invoke a method on it, you will hit by a null pointer exception.
I'd say
if(cell !=null && !cell.isEmpty()) {
_______yes, disply
} else {
_______nope, some thing wrong
}
its not equals(null) its
if(cell != null || !cell.isEmpty())
Try TextUtils.html#isEmpty(java.lang.CharSequence)
I would give try this, seems to be working for me!
if(TextUtils.isEmpty(yourString) && yourString == null){
}
else if(!TextUtils.isEmpty(yourString) && yourString != null){
}
If the value of the string is null, !cell.equals("") will evaluate to true and hence it will go in the if part and not the else part as you are using an OR condition.
NULL != "" (Empty string)!!!
Use this :
if (cell != null && cell.trim().length() > 0) {
// do whatever you want
} else {
// the string received is null
}
Android provides a simple utility
TextUtils.isEmpty(<<stringVariable>>);
More details # http://developer.android.com/reference/android/text/TextUtils.html
WORKING !!!
if (string.matches("")&& string.trim().equals("null")){
return false;
}
else{
return true;
}
I have a question about how to check either the array string got the null value.
My code is like below but still got the string return even the value is null.
for (int i=17;i<29;i++)
{
if (!label[i].equals(null) || !label[i].equals("") || label[i] != null || label[i] != "")
{
Log.d("Get additional label","Additional label = "+label[i]);
}
}
Problem Solved
The problem solved when I change from
if (!label[i].equals(null) || !label[i].equals("") || label[i] != null || label[i] != "")
to
if (label[i].length() != 0)
Thanks for those who replied :)
You could try using the StringUtils api from the apache commons lib:
if(!StringUtils.isEmpty(label[i]))
Check out the docs for more details.
Use == or != to check for null.
e.g. label[i] != null and also the way have it now label[i].equals should end up in NPE if it's really null.
Are you sure your string is actually null and not just an empty string?
This is my code, it always falls into the else even when I know that the value going in (via debugger) is empty.
name = cursor.getString(cursor.getColumnIndex("Genus")) + " " + cursor.getString(cursor.getColumnIndex("Species"));
if(name != "" && name != null)
tv.setText(name);
else
tv.setText("Error");
When doing object comparison, you must use Object.equals(Object) method. Using == or != with Objects will only return true if both Objects reference the same thing. That is why you are falling through to the else.
name.equals("") is probably what you want to use.
Also, things would probably work best if you did something like this:
if(name != null && !"".equals(name))
tv.setText(name);
else
tv.setText("Error");
I know it is an old Question, still contributing so as to have an alternative solution
The below mentioned solution will work if you are inserting "" in ur row while updating it or inserting a new record for a empty field.
if(name.length() > 0 )
tv.setText(name);
else
tv.setText("Error");
The below mentioned solution will work if you are inserting null in ur row while updating it or inserting a new record for a empty field (say edittext).
or if row with no record is created in database already..
if(name != null )
tv.setText(name);
else
tv.setText("Error");
database take the empty cell as null if itz not manually updated later on by inserting "" or some string value.
Both worked for me.
Well Cptcecil
use try - catch error handling mechanism .. instead of if ... else.
Whenever you will try read from empty cursor ... it will throw an exception that you can catch and respond accordingly without breaking your applicaton much.
Thanks