简体   繁体   中英

Error at com.google.common.base.Joiner.on()

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]

The code:

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());
}

I'm trying to accomplish joining an array of strings with a separator, and the option to ignore an argument if desired. However I'm coming across this error that was not there about a week ago. This code was working perfectly.... now its not. I've tried inserting a null check for the "args" variable yet no avail. Has something changed with the package I'm pulling from?

I don't think the args variable is null, rather it contains a null value. Guava's Joiner is null-hostile by default, but it can be configured to tolerate null in one of two ways:

if you don't use either of them, a null value inside your array will throw a NullPointerException, which is explicitly mentioned in the Joiner docs :

If neither skipNulls() nor useForNull(String) is specified, the joining methods will throw NullPointerException if any given element is null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM