IllegalArgumentException:指定为非null的参数为null

时间:2020-12-11 02:32:45

I'm getting the following runtime error:

我收到以下运行时错误:

 checkParameterIsNotNull, parameter oneClickTokens
    at com.info.app.fragments.Fragment_Payment_Profile$fetchMerchantHashes$1.onPostExecute(Fragment_Payment_Profile.kt:0)
    at com.info.app.fragments.Fragment_Payment_Profile$fetchMerchantHashes$1.onPostExecute(Fragment_Payment_Profile.kt:1543)

Here's my code:

这是我的代码:

 private fun fetchMerchantHashes(intent: Intent) {
    // now make the api call.
    val postParams = "merchant_key=$key&user_credentials=$var1"
    val baseActivityIntent = intent
    object : AsyncTask<Void, Void, HashMap<String, String>>() {

        override fun doInBackground(vararg params: Void): HashMap<String, String>? {
                ...
        }

        override fun onPostExecute(oneClickTokens: HashMap<String, String>) {
            super.onPostExecute(oneClickTokens)
            ...

        }
    }.execute()
}

It seems that the function call seems to be invalid. However, I don't know how to fix this problem. Is there anything Kotlin specific I've missed?

似乎函数调用似乎无效。但是,我不知道如何解决这个问题。 Kotlin有什么具体的我错过了吗?

2 个解决方案

#1


57  

The exception is pretty clear: you're passing null for the parameter.

异常非常明确:您为参数传递null。

By default all variables and parameters in Kotlin are non-null. If you want to pass null parameter to the method you should add ? to it's type, for example:

默认情况下,Kotlin中的所有变量和参数都是非空的。如果要将null参数传递给应添加的方法?它的类型,例如:

fun fetchMerchantHashes(intent: Intent?)

For more information: null-safety.

有关更多信息:null-safety。

#2


1  

In my case this error warned about probable passing a null as a parameter. Two ways of correction.

在我的情况下,此错误警告可能将null作为参数传递。两种矫正方式。

  1. Change a type of parameter in corresponding place. If it is received from Java, add @NonNull annotation to a variable definition.
  2. 在相应位置更改参数类型。如果是从Java接收的,请将@NonNull注释添加到变量定义中。

  3. Add !! to a parameter of the method.
  4. 加!!到该方法的参数。

#1


57  

The exception is pretty clear: you're passing null for the parameter.

异常非常明确:您为参数传递null。

By default all variables and parameters in Kotlin are non-null. If you want to pass null parameter to the method you should add ? to it's type, for example:

默认情况下,Kotlin中的所有变量和参数都是非空的。如果要将null参数传递给应添加的方法?它的类型,例如:

fun fetchMerchantHashes(intent: Intent?)

For more information: null-safety.

有关更多信息:null-safety。

#2


1  

In my case this error warned about probable passing a null as a parameter. Two ways of correction.

在我的情况下,此错误警告可能将null作为参数传递。两种矫正方式。

  1. Change a type of parameter in corresponding place. If it is received from Java, add @NonNull annotation to a variable definition.
  2. 在相应位置更改参数类型。如果是从Java接收的,请将@NonNull注释添加到变量定义中。

  3. Add !! to a parameter of the method.
  4. 加!!到该方法的参数。