【发布时间】:2022-01-24 03:13:36
【问题描述】:
我的功能:
fun func(vararg objects: Any?) {
}
当我调用func(null) 时,得到 Exception: Caused by: java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, 参数对象。
而我看到反编译java代码是这样的:
public static final void func(@NotNull Object... objects) {
Intrinsics.checkNotNullParameter(objects, "objects");
printer.v(new Object[]{objects});
}
有没有办法解决这个问题?
【问题讨论】:
-
kotlin 的 vararg 中不能有 null,this kotlinlang 上的帖子证实了这一点。
Note that vararg parameters are, as a rule, never nullable, because in Java there is no good way to distinguish between passing null as the entire vararg array versus passing null as a single element of a non-null vararg array. -
@Alex.T 这看起来像是对我的回答 :-)
-
我已将其添加为答案
标签: kotlin nullpointerexception