为什么Arrays.asList(null)会抛出一个NullPointerException,而Arrays.asList(someNullVariable)不会?
2022-09-02 04:36:56
这个小程序
public class Client {
public static void main(String[] args) throws Exception {
Arrays.asList(null);
}
}
抛出一个 .NullPointerException
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:221)
at java.base/java.util.Arrays$ArrayList.<init>(Arrays.java:4322)
at java.base/java.util.Arrays.asList(Arrays.java:4309)
at org.example.Client.main(Client.java:10)
但是,此程序
public static void main(String[] args) throws Exception {
Arrays.asList(returnNull());
}
private static Object returnNull(){
return null;
}
不。为什么他们的行为不同?