Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有任何内置实用程序可供检查;空";字符串而不是null?_Java_String_Null_Equals - Fatal编程技术网

Java 是否有任何内置实用程序可供检查;空";字符串而不是null?

Java 是否有任何内置实用程序可供检查;空";字符串而不是null?,java,string,null,equals,Java,String,Null,Equals,我正在从ApacheAvroGenericRecord提取一个变量值,因此我的字符串变量值是“null”而不是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.isNullOrEmpt

我正在从ApacheAvro
GenericRecord
提取一个变量值,因此我的字符串变量值是“null”而不是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));

由于这个“null”字符串,我的
sysout
输出为false。我如何检查它,使其打印为“真”bcoz字符串是空字符串。是否有任何内置的utility可以这样做,或者我必须自己编写“.equals”check?

“null”
是一种普通的字符串类型,只需使用
string
类型的API:
.equals(“null”)
就可以了。

您可能自己用
string.valueOf(…)
方法生成
“null”
字符串。从以下文件的JavaDoc:

返回: 如果参数为null,则为等于“null”的字符串;否则,将返回obj.toString()的值

因此,我建议使用以下代码:

    GenericRecord payload = decoder.decode(record.value());
    boolean isNull = payload.get("userId") == null;
    if (!isNull) {
        String userid = payload.get("userId").toString();
    }

这通过不生成字符串来防止与
“null”
字符串进行比较的问题。

您需要修复根本原因。“null”是一个有效字符串,请确保从GenericRecord中获取
null
,而不是
“null”
。谢谢。我怀疑这是可能的。我想这就是Avro模式的定义。是的,你是对的。它来自
String.valueOf