Reputation: 1719
I am checking String whether it's null or not but I don't know why it's always enter into if block
Log.d(TAG,"String ID "+str);
if (str!= null && !str.isEmpty()){
Log.d(TAG,"String ID is not Null ");
} else {
Log.d(TAG,"String ID is Null ");
}
I also generate logs to check String value before checking if condition and it shows like this
01-06 07:17:46.128 30321-30376/com.example D/### String Checker ###: Page ID null
01-06 07:17:46.128 30321-30376/com.example D/### String Checker ###: Page ID is not Null
Update:
Setting String to null
then calling other class like this
String str = null;
new AsyncTask(searchInterfaceChat,getActivity(),String.valueOf(str)).execute(query);
Upvotes: 1
Views: 1296
Reputation: 83008
While looking into your logs, it seems the value of your string is "null". your string is not null. Check into that. This observation is only for your logs
After your update of code, problem is String.valueOf(str)
, it is converting null to "null" which is actually a String. Only pass str
, not String.valueOf(str)
,
like new AsyncTask(searchInterfaceChat,getActivity(), str).execute(query);
Updates for little bit explanation
If you look into code String.valueOf()
, it returns "null"
not null
if in case of null Object. So for you, it is actually "null"
which is being printed correctly on the log, but making confusion. Below is code
/**
* Returns the string representation of the {@code Object} argument.
*
* @param obj an {@code Object}.
* @return if the argument is {@code null}, then a string equal to
* {@code "null"}; otherwise, the value of
* {@code obj.toString()} is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
You can read Difference between null and empty ("") Java String which not exact QA, but you can get some idea about "null"
and null
Upvotes: 3
Reputation: 1026
Bring the null first so that It don't give error.
When str
is not null
it's ok
But
if str
is null
and you write str!=null
the it will first try to read str
but it is null
so it will give an error.
if (null!=str && !str.isEmpty()){
Log.d(TAG,"String ID is not Null ");
} else {
Log.d(TAG,"String ID is Null ");
}
Upvotes: 1
Reputation: 3620
if we want to know, we can look at the source code. the String isEmpty
method returns true if the string length is zero.
public boolean isEmpty() {
// Android-changed: Get length from count field rather than value array (see above).
// Empty string has {@code count == 0} with or without string compression enabled.
// return value.length == 0;
return count == 0;
}
the method String.valueOf
will return a "null" if you object is null.
/**
* @return if the argument is {@code null}, then a string equal to
* {@code "null"}; otherwise, the value of
* {@code obj.toString()} is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
so when you use it in other class, it return the string not null.
Upvotes: 2
Reputation: 1420
Why not to use TextUtils.isEmpty() it will check both of your condition.
if (!TextUtils.isEmpty("your string")){
Log.d(TAG,"String ID is not Null ");
} else {
Log.d(TAG,"String ID is Null ");
}
Also pass the string because in you case you are doing with th string class.
Tried with String s=null and s="ad" value working fine in both case :
public class Main
{
public static void main(String[] args) {
String str ="ss";
if (str!=null&&!str.isEmpty()){
System.out.println("not Null");
} else {
System.out.println(" Null");
}
}
Upvotes: 1
Reputation: 44844
You are passing the String value null as this is what will be returned by String.valueOf(str);
when str
is null
Upvotes: 4