Reputation:
I am extracting a variable value from Apache Avro GenericRecord
so my String variable value is coming as "null" instead of null.
GenericRecord payload = decoder.decode(record.value());
String userid = String.valueOf(payload.get("userId"));
// here userid is null string as "null"
System.out.println(Strings.isNullOrEmpty(userid));
And because of that "null" string, my sysout
prints out as false. How can I check this so that it prints out as "true" bcoz string was a null string. Is there any built in utitilty which does that or I have to write my own ".equals" check?
Upvotes: 1
Views: 403
Reputation: 2319
"null"
is a plain string type, just use the API of String
type: .equals("null")
is ok.
Upvotes: 1
Reputation: 4620
You probably generate the "null"
String by yourself with the String.valueOf(...)
method. From the JavaDoc of String#valueOf
:
Returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
Thus, I suggest to use the following code:
GenericRecord payload = decoder.decode(record.value());
boolean isNull = payload.get("userId") == null;
if (!isNull) {
String userid = payload.get("userId").toString();
}
This prevents the problem of comparing with the "null"
String by not generating it.
Upvotes: 1