Kotlin <-> Java互调问题:Parameter specified as non-null is null

时间:2025-02-08 22:13:53

Kotlin <-> Java互调问题:Parameter specified as non-null is null

1.问题原因

通常是Java调用Kotlin代码在传递参数时出现的问题,Kotlin中设置参数类型不可为空,但是Java调用时传递了null导致。

举例说明:
以Glide加载图片为例

var url:String?=null
     (context).load(url).listener(object : RequestListener<String?, GlideDrawable?> {
            override fun onException(e: Exception, model: String?, target: Target<GlideDrawable?>?, isFirstResource: Boolean): Boolean {
              //...省略
                return false
            }

            override fun onResourceReady(resource: GlideDrawable?, model: String?, target: Target<GlideDrawable?>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
             	//...省略
                return false
            }
        }).into(imageView)

在onException方法中参数e:Exception为不可空类型,我们看下编译之后的代码。

     (context).load(url).listener((RequestListener)(new RequestListener() {
                public boolean onException(@NotNull Exception e, @Nullable String model, @Nullable Target target, boolean isFirstResource) {
                   (e, "e");
                   //...省略
                   return false;
                }

                // $FF: synthetic method
                // $FF: bridge method
                public boolean onException(Exception var1, Object var2, Target var3, boolean var4) {
                   return (var1, (String)var2, var3, var4);
                }

                public boolean onResourceReady(@Nullable GlideDrawable resource, @Nullable String model, @Nullable Target target, boolean isFromMemoryCache, boolean isFirstResource) {
                 //...省略
                   return false;
                }

                // $FF: synthetic method
                // $FF: bridge method
                public boolean onResourceReady(Object var1, Object var2, Target var3, boolean var4, boolean var5) {
                   return ((GlideDrawable)var1, (String)var2, var3, var4, var5);
                }
             })).into();

最重要的代码为 (e, “e”); 在该方法中会进行参数是否为空的校验。

  public static void checkParameterIsNotNull(Object value, String paramName) {
        if (value == null) {
            throwParameterIsNullException(paramName);
        }
    }
    private static void throwParameterIsNullException(String paramName) {
        StackTraceElement[] stackTraceElements = ().getStackTrace();

        // #0 ()
        // #1 
        // #2 
        // #3 our caller
        StackTraceElement caller = stackTraceElements[3];
        String className = ();
        String methodName = ();

        IllegalArgumentException exception =
                new IllegalArgumentException("Parameter specified as non-null is null: " +
                                             "method " + className + "." + methodName +
                                             ", parameter " + paramName);
        throw sanitizeStackTrace(exception);
    }

如果参数为null最终就会抛出*"Parameter specified as non-null is null: " + "method " + className + “.” + methodName + ", parameter " + paramName);异常*。

2.解决办法

解决的办法也很简单,只要在Java调用Kotlin时注意参数是否可空就可以了。如果参数可以为null那就就要在Kotlin代码中加上?以标示为可空类型。