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;
}
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
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.
So here's a snippet of code I'm working on:
String direction = s.readLine();
System.out.println(direction);
if (direction.equals("up") != true && direction.equals("down") != true &&
direction.equals("left") != true && direction.equals("right") &&
direction.equals(null) != true) {
System.out.println("Invalid Solution file");
System.exit(0);
}
What it is supposed to do is read a line from a text file (using a BufferedReader) and then if the line isn't either a valid direction or blank then it should print "Invalid Solution" and exit.
The problem is that no matter what the direction string is the if statement still runs. I put in a println to check whether the direction was being read correctly but it seems absolutely fine. So why isn't the code working as intended?
Part of your problem is readability. Fix that and your problem is 90% solved:
private static List<String> DIRECTIONS = Arrays.asList("up", "down", "left", "right");
then
if (!DIRECTIONS.contains(direction)) {
System.out.println("Invalid Solution file");
System.exit(0);
}
The other 10% was how to check for null, which is direction == null, but if you use this code you don't need to, because contains(null) will conveniently return false.
You code is much more complex than it is needs to.
Consider this instead:
Set<String> validDirections = new HashSet<>(Arrays.asList("up", "down", ...
if (validDirections.contain(direction.toLowerCase()) {
// good ...
} else {
// bad ..
}
You can make validDirections a global constant for example; so it could be used in other places as well.
What I am trying to explain here is: your code is low-level. Low level code is hard to write, read, maintain and extend. Programming is always about creating good abstractions. Or vice versa: if you don't use abstractions, you end up with pretty abstract code, like the one you are showing here!
For example: if you need another direction, you have to put into your already way too complicated if condition. In my solution, you just put it into the statement that builds that Set.
Finally: your error message, is saying nothing. So, that string is bad; but why is it? Wouldn't it be better to at least print the string that caused the error?!
Here && direction.equals("right") I think you have done a mistake since it is on contradiction with the rest :
direction.equals("up") != true &&
direction.equals("down") != true &&
direction.equals("left") != true
You test the negation in the most of conditions but direction.equals("right") tests the affirmation.
Try it , it's the same thing but less verbose and more readable :
if (direction !=null && !direction.equals("up") &&
!direction.equals("down") &&
!direction.equals("left") &&
!direction.equals("right") ){
System.out.println("Invalid Solution file");
System.exit(0);
}
First, you should not use != true with a boolean statement, it is bad form. Rewrite like this:
direction !=null &&
!direction.equals("up") &&
!direction.equals("down") &&
!direction.equals("left") &&
!direction.equals("right")
Your error was that you did not include the != true part on one of your statements within the compound if. Replace with the above code to solve the issue.
I'm confused why you are using !=true when your .equals method already returns a boolean. Try this.
String direction = s.readLine();
System.out.println(direction);
if ( direction!=null && !direction.equals("up") && !direction.equals("down")&& !direction.equals("left")&& direction.equals("right")){
System.out.println("Invalid Solution file");
System.exit(0);
}
Try the following code:
boolean match = false;
if (direction.equals("up"))
{ match = true; }
if (direction.equals("down"))
{ match = true; }
if (direction.equals("left"))
{ match = true; }
if (direction.equals("right"))
{ match = true; }
if (direction.equals(null))
{ match = true; }
if (match == false){
System.out.println("Invalid Solution file");
System.exit(0);
}
You might also want to trim the direction string after reading from file.
The quals method returns a boolean so the result does not need to be compared with the true or false value. Also, I would start with null comparison - boolean expressions in Java are shortened so if this part will be fulfilled rest of the expression is not evaluated. The correct expression might look like this:
if (direction == null || (!direction.equals("up") && !direction.equals("down") && !direction.equals("left") && !direction.equals ("right "))) {
}
But this code is not readable. You could use enums or list of Strings like below
List<String> directions = Arrays.asList("up", "down", "left", "right");
String direction = "readValue"
if (!directions.contains(direction)) {
System.out.println("Invalid direction");
System.exit(0)
}
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