java.lang.NullPointerException: null
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770) ~[spigot-1.12.2.jar:git-Spigot-c3093ef-7e94e65]
at com.google.common.base.Joiner.toString(Joiner.java:454) ~[spigot-1.12.2.jar:git-Spigot-c3093ef-7e94e65]
at com.google.common.base.Joiner.appendTo(Joiner.java:106) ~[spigot-1.12.2.jar:git-Spigot-c3093ef-7e94e65]
at com.google.common.base.Joiner.appendTo(Joiner.java:154) ~[spigot-1.12.2.jar:git-Spigot-c3093ef-7e94e65]
at com.google.common.base.Joiner.join(Joiner.java:197) ~[spigot-1.12.2.jar:git-Spigot-c3093ef-7e94e65]
at com.google.common.base.Joiner.join(Joiner.java:187) ~[spigot-1.12.2.jar:git-Spigot-c3093ef-7e94e65]
at com.google.common.base.Joiner.join(Joiner.java:205) ~[spigot-1.12.2.jar:git-Spigot-c3093ef-7e94e65]
代码:
public static String join(String[] args, int ignored, String separator) {
if (ignored > -1)
args = (String[]) ArrayUtils.remove(args, ignored);
String combined = Joiner.on(separator).join(args); // ERROR HERE
return combined.substring(0, combined.length());
}
我正在尝试完成使用分隔符连接字符串数组,以及如果需要的话忽略参数的选项。然而,我遇到了这个大约一周前不存在的错误。这段代码运行得很好......现在不行了。我尝试为“args”变量插入空检查,但无济于事。我从中提取的包有什么变化吗?
最佳答案
我不认为 args 变量为 null,而是它包含一个 null 值。 Guava 的 Joiner 默认情况下是反对 null 的,但可以通过以下两种方式之一将其配置为容忍 null:
Joiner.on(分隔符)。
skipNulls()
Joiner.on(分隔符)。
useForNull(placeHolder)
如果你不使用它们中的任何一个,数组中的空值将抛出 NullPointerException,这在the Joiner
docs中明确提到。 :
If neither
skipNulls()
noruseForNull(String)
is specified, the joining methods will throwNullPointerException
if any given element isnull
.
关于java - com.google.common.base.Joiner.on() 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459385/